Move the uploaded file to its destination directory is very important via the back-end. PHP provides move_uploaded_file() method by default to move uploaded file.
In PHP uploaded file first goes to temp folder of server. The server discards uploaded files from the temp folder if not moved after uploading.
So, moving the uploaded files from temp to the destination folder is very important. Here, we have to use the move_uploaded_file() function of core PHP to move uploaded files to the destination directory.
We just have to pass a temporary location and destination directory of an uploaded file in move_uploaded_file() method.
move_uploaded_file("TEMPORARY_PATH", "DESTINATION_PATH");
Note: Temporary location can be found by $_FILES[‘field_name’][‘tmp_name’], where field_name should be file field name in the form and tmp_name should be changed.
Read More: How to Upload File in PHP
Example
<?php // Destinaiton path $destination_path = "YOUR_PATH"; // Concatenating uploaded file name with destination path $destination_path = $destination_path.basename($_FILES['file']['name']); // Moving uploaded file if(move_uploaded_file($_FILES['file']['tmp_name'], $destination_path)) { echo "File moved successfully."; } else{ echo "Error, file not moved."; } ?>
Explanation
In the above example, we first defined a variable containing the destination folder. After that, we found the file name of an uploaded file with basename() method and concatenated it with the destination path. Finally, we moved the uploaded file with the move_uploaded_file() method.
Now we have understood the complete usage of move_uploaded_file function. We found how to get the filename of an uploaded file with the basename() method.
Enjoy Coding 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…