Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a server that allows file uploads. Foolishly I let everything get stored in one directory /uploads. Now that I am approaching 1.5M files in this directory, I want to go back and correct my mistake. I am going to move all files into date based directories like so /uploads/2/14/file.ext. How can I do this without ruining all links that currently exist to the files? If someone tries to access site.com/uploads/file.ext it needs to show them the content from site.com/uploads/2/14/file.ext. How do sites like Imgur make it seem as if all their files are stored in one directory?

Things I have thought of:

  • .htaccess redirect (I think this would be way too many URLs to store in this type of file).
  • MySQL based redirect (I think this would cause way too much load on the DB as there are millions of files viewed each day.
share|improve this question
add comment

1 Answer

I would advise to keep a MySQL table that saves the mapping of the file id, to the directory it is kept in. You shouldn't be worried about the DB load considering 1.5M rows and millions of queries per day. For such a simple 1:1 link query. Just make sure you add the proper index to the key you search according to, and if you wish to be better prepared for scale, keep the key an number.

share|improve this answer
    
Could you go into a little more detail about properly indexing the new table? Thank you! –  Josh Mountain Feb 8 at 11:13
    
@JoshMountain Do you have any experience with MySQL? You just need to create an index on the key you will be querying by. I might be able to assist if you include details about you current table –  Noam Feb 9 at 8:13
    
I do have some yes, I just wasn't sure what type of key would be best for this. A new table would be created for this purpose, it would contain a file ID, a file name, possibly a datestamp, and probably a url path I will generate when the files move. –  Josh Mountain Feb 10 at 9:02
    
@JoshMountain Sounds great. For performance a key on INT is better than VARCHAR, but in your case (1.5M rows) I wouldn't worry about that too much currently. –  Noam Feb 10 at 10:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.