Reputation: 1594
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);
}
?>
Upvotes: 4
Views: 24700
Reputation: 1
Change this line ----
$target_path = $target_path. basename( $_FILES['uploadedfile']['name']);
With these ----
$target_path = $_SERVER["DOCUMENT_ROOT"].'/folder_name/'. $_FILES['uploadedfile']['name']);
This works for me.
Upvotes: 0
Reputation: 79
I got same error in my project there is not any fault in your code but there is issue in file name like i can't upload image named ("IMAG0889.jpg", "IMAG0892.jpg", "IMAG0893.jpg" etc) it gave me error 0. after rename file like("Deep2.jpg", "IMG_20160925_093715_1.jpg") I successfully uploaded my image file. So try to upload file after rename it.
Upvotes: 0
Reputation: 30555
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.error
field 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 that move_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.
Upvotes: 10