13
votes

Hi I tried to search online but really could not find it. So what is the difference between $_FILES['file']['name'] and $_FILES['file']['tmp_name'], or what is the $_FILES['file']['tmp_name'] exactly?

According to http://php.net/manual/en/features.file-upload.post-method.php,

$_FILES['userfile']['name'] The original name of the file on the client machine.

$_FILES['userfile']['tmp_name'] The temporary filename of the file in which the uploaded file was stored on the server.

So what is the difference between the temporary filename and original filename?

Thank you

4
the temporary file gets deleted automatically on successful upload. Original name. well... keeps the name of the file uploaded. It's all in the manual php.net/manual/en/features.file-upload.post-method.php. "The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed."Funk Forty Niner
^ Might want to add that the tmp_name is where the file is stored before you use move_uploaded_file()Devon
@Devon I've no need to quote the "entire" manual ;-) It won't fit in here lolFunk Forty Niner

4 Answers

20
votes
$_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!

12
votes

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.

6
votes

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

1
votes

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.