title: Storing Link to File in a Database
body: <p>I am creating a database application that (among many other things) allows users to upload and download files.  The files are stored on a file server and I have set up an Apache HTTP server with PHP scripts to process (i.e. upload and download) the files.  The database only stores a link to the file and not the file itself.  My question is this: How should I organize the files on my file server?</p>
  
  <p>Currently, I am creating a directory structure based on the current date and I rename the files with an MD5 hash of the current date/time (including milliseconds) plus some random characters (i.e. I'm adding "salt"):</p>
  
  <pre><code>\\yyyy\mm\dd\debb40da158040e4f3b93f9576840c07
  </code></pre>
  
  <p>This (above) is the link that is stored in the database (of course, I also store the real file name in the database so that I can rename the file when the user goes to download it---the user never sees the actual link).</p>
  
  <p>I use <code>yyyy\mm\dd</code> for the directory structure to avoid performance issues (I'm told that a lot of files in the same directory can slow things down) and I rename the files with a unique string to avoid clashes when users upload files with the same name.</p>
  
  <p>I'd like to get other opinions on the best way to deal with storing files in this kind of situation.  I've seen some developers keep the file name, but append (as a prefix) the database ID of the corresponding row in the file information table---I see some advantages to this approach since the file names are "human readable" and you can figure out what the files are if the database file information table ever got corrupted or deleted.</p>