title: PHP mechanism to prevent users from uploading the same file twice
body: <p>I'm trying to allow users to upload files through a PHP website. Since all the files are saved in a single folder on the server, it's conceivable (though admittedly with low probability) that two distinct users could upload two files that, while different, are named exactly the same. Or perhaps they're exactly the same file.</p>
  
  <p>In the both cases, I'd like to use <code>exec("openssl md5 " . $file['upload']['tmp_name'])</code> to determine the MD5 hash of the file immediately after it is uploaded. Then I'll check the database for any identical MD5 hash and, if found, I simply won't complete the upload.</p>
  
  <p>However, in the <code>move_uploaded_file</code> documentation, I found this comment:</p>
  
  <blockquote>
    <p>Warning: If you save a md5_file hash in a database to keep record of uploaded files, which is usefull to prevent users from uploading the same file twice, be aware that after using move_uploaded_file the md5_file hash changes! And you are unable to find the corresponding hash and delete it in the database, when a file is deleted.</p>
  </blockquote>
  
  <p>Is this really the case? <strong>Does the MD5 hash of a file in the tmp directory change after moving it to a permanent location?</strong> I don't understand why it would. And regardless, is there another, better way of ensuring the same file is not uploaded to the filesystem multiple times?</p>