How can the move_uploaded_file() function be modified to ensure the correct file name is appended to the destination path?

When using the move_uploaded_file() function in PHP, it is important to ensure that the correct file name is appended to the destination path to avoid overwriting existing files or causing errors. To do this, you can use the basename() function to extract the file name from the uploaded file path and append it to the destination path.

// Get the file name from the uploaded file path
$fileName = basename($_FILES["fileToUpload"]["name"]);

// Define the destination path with the correct file name appended
$destination = "uploads/" . $fileName;

// Move the uploaded file to the destination path
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $destination)) {
    echo "File uploaded successfully.";
} else {
    echo "Error uploading file.";
}