If you are new to PHP and just started learning the concepts, then definitely you’re gonna face this error once:
“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”
When you try to upload any file or document through html form and php, you required that file to move physically to your destination folder.
PHP provides a function called move_uploaded_file(filename,destination) to move your uploaded file from temporary folder to required folder.
1st Parameter is a filename that is 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;
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.
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().
Understand the basic requirement of the function and you will hardly face any error!
Hope this helps you. Best of luck for you code!