Tuesday, March 6, 2012

Efficient file storage for user uploaded files

While disk space is cheap, dealing with large numbers of files can be tedious, and in some cases unnecessary.  Before considering file naming conventions, consider the uniqueness of your files.  At work, we allow our customers to upload logos.  It's possible for multiple customers to upload the same logo.  The last thing we wanted was to maintain multiple copies of the same logo on the server.

We decided our best approach would be to name the files based on the contents of the file.  If the contents changed, so did the filename.

$filename = md5(file_get_contents($pathToFile));

The MD5 hashed filename is what was linked to the user logos.  This turned out to be a fantastic solution for our needs.

What naming conventions do you use for user uploaded files?  This method seems like it would work well for any uploaded files because nothing would ever be the same.  Do you agree or disagree?  Why?

No comments:

Post a Comment