How can JavaScript be used to prevent deleted files from being uploaded in a PHP file upload form?

When a file is deleted from the server, there is a risk that it could still be uploaded by a user through a form. To prevent this, JavaScript can be used to check if the file exists on the server before allowing it to be uploaded in a PHP file upload form. This can be done by sending an AJAX request to the server to check if the file exists before submitting the form.

<?php
if(isset($_FILES['file'])){
    $file = $_FILES['file'];

    // Check if file exists on the server
    $file_path = 'uploads/' . $file['name'];
    if(!file_exists($file_path)){
        // File does not exist, do not upload
        echo "File does not exist on the server.";
    } else {
        // File exists, proceed with upload
        move_uploaded_file($file['tmp_name'], $file_path);
        echo "File uploaded successfully.";
    }
}
?>