What is the difference between name and tmp_name

Your file will temporary store on this path $_FILES['image_path']['tmp_name']. so when you move it will be remove from temp folder to your folder. If you use copy command instead of move_uploaded_file then your temp file will be stay in your server's temp folder. you can search file name in there.


tmp_name is the temporary name of the uploaded file which is generated automatically by php, and stored on the temporary folder on the server.

name is the original name of the file which is store on the local machine.


when you send a file to a server-side script (php or asp or...), server will upload and move your file to a temporary directory of itself until the processing of the script file is done. then it will remove the file from that directory. so $_FILES['file']['tmp_name'] is the path (not name) of that temporary file

so lets examine/see this: since the processing of a php file especially on a virtual server like xampp is very fast so we can't see tmp file when it's created. so we use sleep() function of php to see what is happening exactly, here we have a single page containing a very simple php code which is here and this is what happening:

  1. we choose a file (noting has been sent yet)
  2. we press upload button (file will upload to tmp directory of server)
  3. after (4+ε) seconds , runing php script will finish and the temporary file will be removed from tmp directory
<?php
if (isset($_POST['submit'])) {
    sleep (4);
    echo $_FILES['fileToUpload']['tmp_name'];
}

?>
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
    <form action="" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input onchange="uImage(event)" type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
</body>
</html>

enter image description here

note1: for php servers you can find your server temporary path in php.ini file. it's the value of upload_tmp_dir in that file


$_FILES['file']['tmp_name']

Provides the name of the file stored on the web server’s hard disk in the system temporary file directory, unless another directory has been specified using the upload_tmp_dir setting in your php.ini file. This file is only kept as long as the PHP script responsible for handling the form submission is running. So, if you want to use the uploaded file later on (for example, store it for display on the site), you need to make a copy of it elsewhere.

To do this you can use the move_uploaded_file() function which moves an uploaded file from its temporary to permanent location. Please note that you'd best use move_uploaded_file() over functions like copy() and rename() for this purpose because it performs additional checks to ensure the file was indeed uploaded by the HTTP POST request.

$_FILES['file']['name']

Provides the name of the file on the client machine before it was submitted.If you make a permanent copy of the temporary file, you might want to give it its original name instead of the automatically-generated temporary filename that’s described above.

So all in all:

$_FILES["file"]["name"] //stores the original filename from the client
$_FILES["file"]["tmp_name"] //stores the name of the temporary file

Hope it helps!

Tags:

Php