PHP loading problem Getting error 0, but move_uploaded_file () returns false

PHP loading error Error while getting 0, but move_uploaded_file () returns false. When I print $ _FILES, I get

Array ( [uploadedfile] => Array ( [name] => flashlog.txt [type] => text/plain [tmp_name] => /tmp/php0XYQgd [error] => 0 [size] => 3334 ) ) 

I am using the basic html / php tutorial, which makes me think that this might be a server problem. I check php.ini and have upload_max_filesize: 2M, post_max_size: 8M. So I'm really confused as I thought error 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); } ?> 
+4
source share
2 answers

move_uploaded_file() will also return false if it cannot write to the target directory.

Most of the PHP code that I see for handling downloads skips checking some important part of the process. The download code should follow these steps:

  • Make sure $_FILES[] exists and the required entry is populated.
  • Look in the error field to see if he got to the server - a lot of code just checks that it is 0, which means that he cannot return any worthy error to the user.
  • Make sure that the destination where you need to move the file really exists.
  • Call move_uploaded_file() to perform the move - too many just copy the file, which bypasses the security checks that move_uploaded_file() does.

These are discrete steps: as you seem to see, the actual download may succeed, but move_uploaded_file() may fail. Your question suggests that if the latter failed, then the first.

Oh yes: call move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $destination) . Using $_FILES['uploadedfile']['name'] will not work.

+9
source

I have the same error in my project, there is no error in your code, but there is a problem in the file name, since I can’t upload the image with the name ("IMAG0889.jpg", "IMAG0892.jpg", "IMAG0892.jpg" , IMAG0893.jpg ", etc.), it gave me an error 0. after renaming the file (" Deep2.jpg "," IMG_20160925_093715_1.jpg "). I successfully uploaded my image file. So try to download the file after renaming .

0
source

Source: https://habr.com/ru/post/1316492/


All Articles