title: User file upload: Check if file exists before upload (prompt overwrite yes/no)
body: <p>I have a simple form that allows users to choose a file then upload it.
  How can I check if the file exists before the file is uploaded, if it exists, prompt the user to confirm the overwrite (yes - overwrite, no - don't do anything/cancel). I have heard it's best to use ajax, but I have little experience with ajax and can't find an example to implement.</p>
  
  <p>Form code:</p>
  
  <pre><code>&lt;form method='post' enctype='multipart/form-data' action='&lt;?php $_server['PHP_SELF']; ?&gt;'&gt;
        &lt;input type='file' name='file_upload'&gt;          
        &lt;br&gt;&lt;br&gt;
        &lt;input type='submit' name='submit' value="Upload"&gt;
        &lt;/form&gt;
  </code></pre>
  
  <p>My attempt: </p>
  
  <pre><code>if(isset($_POST['submit'])) {
    if(file_exists($fullFileName)){ 
      echo '&lt;script&gt;
           if (!confirm("Do you want to overwrite the existing file?")) {
             window.location.replace(window.location.href);
           }
           &lt;/script&gt;';
           unlink($fullFileName);
           if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], '' . $_FILES['file_upload']['name'])){
             die('Errrrrr! Error uploading file - check destination is writeable.');
           }
    }
      else {
        if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], '' . $_FILES['file_upload']['name'])){
                die('Errrrrr! Error uploading file - check destination is writeable.');
        }
      }
  }   
  </code></pre>
  
  <p>My code overwrites the file even if 'Cancel' is selected at the confirm. Thanks!</p>