54

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?

6
  • 25
    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. " php.net/manual/en/features.file-upload.post-method.php
    – user557846
    Commented Jun 21, 2012 at 3:48
  • 1
    @Dagon What happens if the script gets interrupted, like if I close the browser or press the back button? Commented Jun 21, 2012 at 3:58
  • 1
    in both cases the file or what ever part of the file was uploaded will be deleted
    – user557846
    Commented Jun 21, 2012 at 3:59
  • 2
    @Dagon unless ignore_user_abort has been set in the API or has been called. Commented Jun 21, 2012 at 4: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.
    – user557846
    Commented Jun 21, 2012 at 4:15

3 Answers 3

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>";

?>
4
  • 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
    Commented Jan 29, 2013 at 0: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
    Commented May 28, 2014 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
    Commented May 28, 2014 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
    Commented Sep 9, 2015 at 5:30
11

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

3
  • How to know when script is finished because I want to apply same behavior on another folder?
    – Katty
    Commented Mar 16, 2017 at 9: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. Commented Jun 21, 2020 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: 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
    Commented Nov 2, 2022 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);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.