52

I am running WAMP server. On file upload using PHP I see

$_FILES[tmp_name] => string 'C:\wamp\tmp\phpD382.tmp' (length=23)

I go to that folder and it's empty. I made sure my 'show hidden files' is on from my 'folders option' but I don't see it. Where is it exactly?

Besides when does it get deleted? If I don't move that file? For an instance if I'm uploading a file and the file uploaded halfway and I decided to close that browser what happens to the file? When does the server know to delete that temp file?

Blackbam
  • 17,496
  • 26
  • 97
  • 150
user1105430
  • 1,379
  • 3
  • 15
  • 27
  • 24
    as soon as the script finishes. "The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed. " http://www.php.net/manual/en/features.file-upload.post-method.php –  Jun 21 '12 at 03:48
  • 1
    @Dagon What happens if the script gets interrupted, like if I close the browser or press the back button? – user1105430 Jun 21 '12 at 03:58
  • 1
    in both cases the file or what ever part of the file was uploaded will be deleted –  Jun 21 '12 at 03:59
  • 2
    @Dagon unless `ignore_user_abort` has been set in the API or has been called. – Bailey Parker Jun 21 '12 at 04:07
  • 1
    @PhpMyCoder i would have thought even in that case closing the browser would stop the file being sent. tested it -yup file upload stops server side cant control that. –  Jun 21 '12 at 04:15
  • @Dagon Point taken. I was assuming the file was already uploaded when the user pushed the back button, but this probably won't always be the case. – Bailey Parker Jun 21 '12 at 04:18

3 Answers3

36

As soon as your PHP script finishes executing and re-saving to the defined location

Example using straight PHP, no framework

http://www.php.net/manual/en/features.file-upload.post-method.php

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>
NDBoost
  • 10,184
  • 6
  • 53
  • 73
  • 1
    on some WAMP installations your `$uploaddir` might be something like `C:/wamp/www/uploads/` and the `$_FILES` array might be using `file`, not `userfile` – tim Jan 29 '13 at 00:26
  • Considering the file isn't re-saved in a defined location (ex: read an excel file, get the data -> insert to db, toss the file), does php destroy the tmp file anyway at the end of the script? – jlcharette May 28 '14 at 20:17
  • 5
    Nvm found it: The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed. – jlcharette May 28 '14 at 20:21
  • @gorelative What happens if I rename the file within the tmp dir? Does it too get deleted after the script execution finished ? – edam Sep 09 '15 at 05:30
11

If you do not do anything with them they will be deleted right after the script is finished.

Amir Hassan Azimi
  • 9,180
  • 5
  • 32
  • 43
  • How to know when script is finished because I want to apply same behavior on another folder? – Katty Mar 16 '17 at 09:24
  • This is a late reply. But it could help others. You could try registering a callback function with the register_shutdown_function. This function gets invoked when a script ends. A script could also end when there is a fatal error. This could also be caught inside the function and steps taken as needed. – Sheikh Azad Jun 21 '20 at 13:46
  • Do you have a source for your statement? And what happens when the script gets interrupted with a fatal error? Found this: https://www.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. – user706420 Nov 02 '22 at 10:20
7

If deleting file is not desired, i found that PHP wont delete file after execution if you "move" it to same location.

move_uploaded_file($temporaryFile, $temporaryFile);
Aiphee
  • 8,904
  • 3
  • 18
  • 16