PHP Warning: move_uploaded_file() unable to move

PHP is very popular nowadays among the coding languages and very easy to use for any beginer having little to no knowledge.

Though, while learning the basic PHP concepts, it’s possible that you face some common errors of which you aren’t aware. One of such very familiar error is:

Warning: move_uploaded_file(upload/5c10de74dbb1b4.82572195.png): failed to open stream: No such file or directory in C:\wamp64\www\errortesting\invalid-foreach-argument.php on line 26

Warning: move_uploaded_file(): Unable to move ‘C:\wamp64\tmp\php505.tmp’ to ‘upload/5c10de74dbb1b4.82572195.png’ in C:\wamp64\www\errortesting\invalid-foreach-argument.php on line 26

In PHP, when you try to upload any file or document through html form, you required to move that file physically to your destination folder as well.

PHP provides a function for that called, move_uploaded_file(filename,destination) to move your uploaded file from temporary folder to required folder.

1st Parameter is a filename, the temporary name of the file in $_FILES array.

2nd Parameter is a destination folder where you want the file to be moved. For ex, $destination = ‘uploads/new_filename’;

How the error is generated?

Look into the below case:

$file_tmp = $_FILES['file']['tmp_name'];
$file_name = $_FILES['file']['name'];
$file_destination = 'upload/' . $file_name;
move_uploaded_file($file_tmp, $file_destination);

In above code snippet, I have used $_FILES array with the name of the input field that is “file” to get the name and temporary name of the uploaded file.

$_FILES is a global 2-dimensional array to handle POST requests of file fields which stores the basic file informations such as file name, file size, file type and other important attributes.

Check the scenario that cause the error:

  • Make sure you have created folder where you want to move your file.
  • Check for the folder permission, is it writable or not. Use chmod() to modify permission.
  • See if your target path URL is properly created or not. You can use dirname(__FILE__) to get the absolute path. To get current working directory, use getcwd().

The file destination should have absolute path to your upload folder and the folder in which you want to move the file should be created with necessary permissions.

With the knowledge of basic requirements of the function, you will hardly face any error!

Hope this helps you. Best of luck for you code!