How can the move_uploaded_file() function be used to save files in PHP?
The move_uploaded_file() function in PHP is used to save files that have been uploaded via a form to a specified directory on the server. This function takes two parameters: the temporary location of the uploaded file and the destination where the file should be saved. It is important to ensure that the destination directory has the appropriate permissions set to allow the file to be saved successfully.
<?php
$target_dir = "uploads/"; // specify the directory where the file should be saved
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); // get the file name
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
?>