Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Upload not in /tmp

Tags:

php

This sounds like a stupid question but, after uploading a file the file isnt in the location php said it would be.

First the simple test page:

<html><body> 
<h1><?=$_FILES['imgup']['tmp_name'];?></h1>
<?
        print_r($_FILES);
?>
<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="file" name="imgup" id="imgup">
<input type="submit">
</form>
</body></html>

Now, the print_r in plain text:

Array ( [imgup] => Array ( [name] => ace.jpg [type] => image/jpeg [tmp_name] => /tmp/phpEdfBjs [error] => 0 [size] => 29737 ) )

So no error, standard looking path, but /tmp has no such file. Further, running a find on my whole system turns up bubkis.

FYI: php.ini has

max_execution_time = 120
file_uploads = On
upload_max_filesize = 2M

and the file I've been uploading is 29k

Any thoughts?

like image 873
Frank Conry Avatar asked Apr 01 '11 16:04

Frank Conry


1 Answers

The temporary file is deleted when the PHP script that received it has finished running : it is just a temporary file.

The PHP script to which you are posting the form -- upload.php -- should move the temporary file to a non-temporary location, using move_uploaded_file()


Basically, the idea is :

  • The user uploads a file to your script,
    • That file is stored in a temporary lcoation.
  • Your script works on that file :
    • Checks if the file is OK (content-type, size, ...)
  • And if the file is OK, your script moves it to the permanent storage directory (whereever you want)

If the upload doesn't finish successfully, or if you don't move the file somewhere else, the temporary file is automatically deleted.


As a reference, you should read the following section of the manual : Handling file uploads - POST method uploads

Quoting the part of it which is related to your problem :

The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.

like image 114
Pascal MARTIN Avatar answered Oct 19 '22 03:10

Pascal MARTIN