PHP:临时上传的文件什么时候被删除?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11131226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:48:43  来源:igfitidea点击:

PHP: When does the temporary uploaded files get deleted?

phpfile-upload

提问by user1105430

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

我正在运行 WAMP 服务器。在使用 PHP 上传文件时,我看到

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

$_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?

另外什么时候删?如果我不移动那个文件?例如,如果我正在上传文件并且文件上传到了一半,而我决定关闭该浏览器,文件会发生什么情况?服务器什么时候知道要删除该临时文件?

采纳答案by gorelative

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

一旦您的 PHP 脚本完成执行并重新保存到定义的位置

Example using straight PHP, no framework

使用直接 PHP 的示例,无框架

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

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

?>

回答by Hassan Azimi

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

如果您不对它们进行任何操作,它们将在脚本完成后立即被删除。

回答by Aiphee

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

如果不需要删除文件,我发现如果您将其“移动”到同一位置,PHP 不会在执行后删除文件。

move_uploaded_file($temporaryFile, $temporaryFile);