What are some potential pitfalls when overwriting files in PHP during uploads?

One potential pitfall when overwriting files in PHP during uploads is the risk of accidentally overwriting important files or allowing malicious users to upload harmful files. To prevent this, you can add a check to see if the file already exists before overwriting it. If the file exists, you can either rename the new file or prompt the user to confirm the overwrite.

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

if (file_exists($uploadFile)) {
    // Prompt user to confirm overwrite or rename the file
} else {
    move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile);
    echo "File uploaded successfully!";
}