Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between name and tmp_name

Tags:

php

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

like image 762
James Chen Avatar asked May 03 '16 15:05

James Chen


People also ask

What is $_ FILES in php?

PHP $_FILES The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.

Which of the following provides error code associated with this file upload?

Error Messages Explained ¶ The error code can be found in the error segment of the file array that is created during the file upload by PHP. In other words, the error might be found in $_FILES['userfile']['error'] .

Which of the following provides the size of the uploaded file in php?

The PHP global $_FILES contains all the information of file. By the help of $_FILES global, we can get file name, file type, file size, temp file name and errors associated with file.


1 Answers

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

like image 52
kia nasirzadeh Avatar answered Sep 22 '22 20:09

kia nasirzadeh