问题
I have a very basic upload script, probably lifted straight off the php.net/move_upload_files function page.
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.
Also $_FILEScontains correct details for the file I have just uploaded.
Had anyone have any idea why the temporary file is being removed from /tmp before I have a chance to handle it?
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
[userfile] => Array
(
[name] => switchsolo.png
[type] => image/png
[tmp_name] => /tmp/phpIyKRl5
[error] => 0
[size] => 6690
)
But /tmp/phpIyKRl5 simply isn't there.
回答1:
1) Are the post_max_size and upload_max_filesize holding higher value than the size of the file you are trying to upload?
2) Does your uploading script take longer time to execute than the value of the max_execution_time variable allows?
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.
These variables can be changed in php.ini and/or .htaccess or with ini_set().
Hope that helps.
回答2:
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.
回答3:
I was trying out http://www.w3schools.com/php/php_file_upload.asp And I stumbled across the same bug.
In my case, adding a "./" before the $destination solved the problem.
bool move_uploaded_file ( string $filename , string $destination )
回答4:
It may also be that you destination folder does not exists or you don't have write permission.
回答5:
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...
I solved the issue by checking the $upload_file path attribute in the move_uploaded_file function - make sure it is correct otherwise it will not work (mine was incorrect).
Also, the file in the temp location gets removed automatically, I think that is the way PHP works.
I hope this helped.
回答6:
Are you 100% sure that the file actually is created in /tmp? If you don't have write permission (or the user the script runs as) the file wont be written to /tmp but (I'm guessing) you'll see it during the upload although it's not actually there when the upload finishes.
Edit: $_FILES['file']['error'] - Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0. So check your PHP-version. If it's below 5.1 write to disk might be your problem.
回答7:
I've had this problem myself. In my case php was uploading to the wrong tmp folder. Instead of using the domains tmp folder (in a virtual host on plesk) it was uploading straight to the OS temporary folder.
Check the settings of your temporary folders
回答8:
For future reference this can also happen when Apache does not have access to the destination directory (Remember to change the ACLs !!).
回答9:
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.
I had to add "E" to my "variables order" in php.ini ; variables_order Default Value: "EGPCS"
Hope this helps.
回答10:
Simple test to do right before the move_uploaded_file() function:
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"]);
回答11:
be careful with your $upload_file. it might have been lack of a "/" when you concatenated folder path and name of file uploaded.
回答12:
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.
回答13:
Your form should use a tag like this:
<form method="post" enctype="multipart/form-data" action="...">
Use multiple/form-data as enctype.
回答14:
You are using the return value of move_uploaded_file() the wrong way round:
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_file))
{
$result['error'] = 'true';
}
else
{
$result['error'] = 'false';
$result['file_loc'] = $upload_file;
}
来源:https://stackoverflow.com/questions/1819563/php-file-upload-files-disappearing-from-tmp-before-move-uploaded-files