PHP Uploading issue Getting Error 0 but move_uploaded_file() is returning false. When I print out $_FILES I get
Array ( [uploadedfile] => Array ( [name] => flashlog.txt [type] =>
text/plain [tmp_name] => /tmp/php0XYQgd [error] => 0 [size] => 3334 ) )
I’m using a basic html/php tutorial which leaves me to believe that it might be a server issue. I check the php.ini and have upload_max_filesize:2M, post_max_size:8M. So I’m really confused as I thought error of 0 told me that it was successful.
The code I’m using is
<?php
// Where the file is going to be placed
$target_path = 'Test/';
$target_path = $target_path. basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
}
else
{
echo "There was an error uploading the file, please try again!";
echo print_r($_FILES);
}
?>
move_uploaded_file()will also return false if it can’t write to the target directory.Most PHP code I see to handle uploads skips checking some major piece of the process. Upload code should do the following steps:
$_FILES[]exists and the correct entry is populated.errorfield to see if it got to the server at all — a lot of code just checks that it’s 0, which means it can’t return any decent error to the user.move_uploaded_file()to do the move – too many just do a file copy, which bypasses the security checks thatmove_uploaded_file()does.These are discrete steps: as you seem to be seeing, the actual upload can succeed, yet
move_uploaded_file()can fail. Your question assumes that if the latter failed, so did the former.Oh yes: call
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $destination). Using$_FILES['uploadedfile']['name']won’t work.