title: File Uploads - best practise for directory structure?
body: <p><strong>Context:</strong></p>
  
  <p>I've just written a PHP file upload handler that saves any incoming file to my site's upload directory.
  The upload directory is publicly accessible through the web, and currently each file is just dumped straight into it after being renamed with a unique ID generated by the database.
  The hander will allow any file type aside from JS and PHP, not just images.
  Number of files likely to be in the thousands eventually but it will take a couple of years for that to happen.</p>
  
  <p><strong>My question:</strong></p>
  
  <p>Is it a good or bad idea to have all uploaded files in the same directory on the server, and if not please explain why, and what would be better approach?</p>
  
  <p>I notice that wordpress stores them in directories named for the date for instance.</p>
  
  <p>The only real downside I can see is that an <code>ls</code> on the directory might be resource intensive once there are a few thousand files in there.
  Or is there limits placed on number of files in one dir, or total size of one dir (running Linux)?</p>
  
  <p><strong>slightly preemptive note about security:</strong> </p>
  
  <p>The upload handler is secured against anyone but logged in administrator level users uploading files, and it also won't save PHP or JS files (atleast as far as I can reliably test for them using the <code>type</code> attribute in the <code>$_FILES</code> array) as these may be executed by the server. Another safeguard I plan to implement is an <code>.htaccess</code> file that blocks requests to certain filetypes, just incase. Any notes on this aspect would also be welcome but it's not part of this question.</p>