How can PHP functions like file_exists, unlink, and rename be utilized in the context of file uploading and renaming?

When uploading files in PHP, it is common to check if a file already exists, delete it if needed, and rename it if necessary. The functions file_exists, unlink, and rename can be utilized for these tasks. First, check if the file already exists using file_exists, then delete it using unlink if needed, and finally rename the uploaded file using rename.

// Check if the file already exists
if (file_exists($targetDirectory . $newFileName)) {
    // Delete the existing file
    unlink($targetDirectory . $newFileName);
}

// Upload the file
move_uploaded_file($_FILES["file"]["tmp_name"], $targetDirectory . $newFileName);