PHP 文件上传,文件在 move_uploaded_files 之前从 /tmp 中消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1819563/
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
PHP File Upload, files disappearing from /tmp before move_uploaded_files
提问by Bowen
I have a very basic upload script, probably lifted straight off the php.net/move_upload_files function page.
我有一个非常基本的上传脚本,可能是直接从 php.net/move_upload_files 函数页面中删除的。
move_uploaded_file()is failed because it cannot find the tmp file in the tmp folder. But I KNOW that it is being put there, but is removed before move_upload_file()can deal with it in my script. I know it is being put there since I can see a file in there when a large file is being posted to the server.
move_uploaded_file()失败,因为它在 tmp 文件夹中找不到 tmp 文件。但是我知道它被放在那里,但是move_upload_file()在我的脚本中可以处理它之前被删除了。我知道它被放在那里,因为当一个大文件被发布到服务器时我可以看到一个文件。
Also $_FILEScontains correct details for the file I have just uploaded.
还$_FILES包含我刚刚上传的文件的正确详细信息。
Had anyone have any idea why the temporary file is being removed from /tmp before I have a chance to handle it?
有没有人知道为什么在我有机会处理临时文件之前从 /tmp 中删除它?
Here is the basic code that I am using.
这是我正在使用的基本代码。
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_file))
{
$result['error'] = 'false';
$result['file_loc'] = $upload_file;
}
else
{
$result['error'] = 'true';
}
The output of print_r($_FILES)looks like
输出print_r($_FILES)看起来像
[userfile] => Array
(
[name] => switchsolo.png
[type] => image/png
[tmp_name] => /tmp/phpIyKRl5
[error] => 0
[size] => 6690
)
But /tmp/phpIyKRl5 simply isn't there.
但是 /tmp/phpIyKRl5 根本就不存在。
回答by Kristinn ?rn Siguresson
1) Are the post_max_size and upload_max_filesize holding higher value than the size of the file you are trying to upload?
1) post_max_size 和upload_max_filesize 的值是否高于您尝试上传的文件大小?
2) Does your uploading script take longer time to execute than the value of the max_execution_time variable allows?
2) 您的上传脚本执行时间是否比 max_execution_time 变量允许的值更长?
3) I assume your uploading script doesn't consume as much memory as the memory_limit variable allows. When the client is uploading the file to the server, then the server is probably holding some of it in memory while doing so. I'm not sure if it somehow affects the limit of the memory_limit variable in php.ini.
3)我假设您的上传脚本不会像 memory_limit 变量允许的那样消耗尽可能多的内存。当客户端将文件上传到服务器时,服务器可能在执行此操作时将其中的一些保存在内存中。我不确定它是否会以某种方式影响 php.ini 中 memory_limit 变量的限制。
These variables can be changed in php.ini and/or .htaccess or with ini_set().
这些变量可以在 php.ini 和/或 .htaccess 中或使用 ini_set() 进行更改。
Hope that helps.
希望有帮助。
回答by Joel L
The file is removed after the script finishes executing. If you run your script, and then check the /tmp/ folder, the file will not be there no matter what.
脚本完成执行后,该文件将被删除。如果您运行脚本,然后检查 /tmp/ 文件夹,无论如何该文件都不会存在。
回答by Mark Stahler
For future reference this can also happen when Apache does not have access to the destination directory (Remember to change the ACLs !!).
为了将来参考,当 Apache 无权访问目标目录时,这也可能发生(记住更改 ACL !!)。
回答by You Help Me Help You
I was trying out http://www.w3schools.com/php/php_file_upload.aspAnd I stumbled across the same bug.
我正在尝试http://www.w3schools.com/php/php_file_upload.asp并且我偶然发现了相同的错误。
In my case, adding a "./" before the $destination solved the problem.
就我而言,在 $destination 之前添加“./”解决了问题。
bool move_uploaded_file ( string $filename , string $destination )
回答by jet
I was just having this issue as well, came across this website for a solution - did not get it tho ;)
我也刚遇到这个问题,来到这个网站寻求解决方案 - 没有得到它;)
I was getting the correct info when viewing print_r($_FILES)but was unable to execute move_uploaded_file...
查看时我得到了正确的信息,print_r($_FILES)但无法执行move_uploaded_file...
I solved the issue by checking the $upload_filepath attribute in the move_uploaded_filefunction - make sure it is correct otherwise it will not work (mine was incorrect).
我通过检查函数中的$upload_file路径属性解决了这个问题move_uploaded_file- 确保它是正确的,否则它将无法工作(我的不正确)。
Also, the file in the temp location gets removed automatically, I think that is the way PHP works.
此外,临时位置中的文件会自动删除,我认为这就是 PHP 的工作方式。
I hope this helped.
我希望这有帮助。
回答by ieeehh
It may also be that you destination folder does not exists or you don't have write permission.
也可能是您的目标文件夹不存在或您没有写入权限。
回答by Stano
Simple test to do right before the move_uploaded_file() function:
在 move_uploaded_file() 函数之前做的简单测试:
if (!file_exists("upload")) {
if (mkdir("upload")) {
echo "Upload directory created!";
}
else {
die( "Invalid upload directory!" );
}
}
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
回答by Sensor
be careful with your $upload_file. it might have been lack of a "/" when you concatenated folder path and name of file uploaded.
小心你的 $upload_file。当您连接文件夹路径和上传文件的名称时,它可能缺少“/”。
回答by Attila Ertekes
Try to comment out PrivateTmp=true in in /etc/systemd/system/multi-user.target.wants/apache2.service After restarting Apache, it will always use system's temporary folder, not a relative one per session.
尝试在 /etc/systemd/system/multi-user.target.wants/apache2.service 中注释掉 PrivateTmp=true 重新启动 Apache 后,它将始终使用系统的临时文件夹,而不是每个会话的相关文件夹。
回答by Prakash Raman
I had the same problem, where 'tmp_name' would say it is being stored in '/private/var/tmp/' but the file would not exist there.
我遇到了同样的问题,其中 'tmp_name' 会说它存储在 '/private/var/tmp/' 中,但该文件不存在那里。
I had to add "E" to my "variables order" in php.ini ; variables_order Default Value: "EGPCS"
我必须在 php.ini 的“变量顺序”中添加“E”;variables_order 默认值:“EGPCS”
Hope this helps.
希望这可以帮助。

