How can the move_uploaded_file function in PHP be used to upload and rename files in a single step?
To upload and rename files in a single step using the move_uploaded_file function in PHP, you can simply specify the desired file name in the destination parameter of the function. This allows you to upload the file from the temporary directory to the desired location with the new name all in one go.
// Example code to upload and rename a file in PHP
$uploadDir = 'uploads/';
$uploadedFile = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
$newFileName = 'new_file_name.jpg'; // Specify the new file name here
if(move_uploaded_file($uploadedFile, $uploadDir . $newFileName)) {
echo "File uploaded and renamed successfully.";
} else {
echo "Error in uploading and renaming file.";
}