How can file upload functionality in PHP be improved to prevent potential security vulnerabilities, such as file overwrite?

To prevent potential security vulnerabilities such as file overwrite, the file upload functionality in PHP can be improved by checking if a file with the same name already exists before moving the uploaded file. If a file with the same name exists, you can either generate a unique filename or prompt the user to rename the file.

$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);

if (file_exists($uploadFile)) {
    // Generate a unique filename or prompt the user to rename the file
} else {
    move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile);
    echo "File uploaded successfully!";
}