Storing Uploaded Files on Server Filesystem with Rails

Storing files in large BLOB columns of your database has its place, but in situations in which that is not the case, one needs a well organized way to keep files on the server. Often times folks will create a folder called “uploaded_files” and dump all of their files into that directory.. which works until you get more than 200 files in that directory – then what!? The directory cannot be copied and cannot be opened remotely because of its size, and even if it can be, how do you find a file if need be — you can’t!

Tonight I put together a very basic upload project to display how I sometimes accomplish the file upload and storage.

http://github.com/loudpixel/simple-rails-file-uploads will show you how to manage files on your server in a well organized way. This method enables you to define a base directory where the files will be stored (e.g. /public/uploaded_files – just like before) but when it saves files to the file system it organizes them in folders in the format of YYYY/MM/DD/ based on the created_at value of the files.

Do you store files a different way, have questions as to why I use this method?

This entry was posted in Ruby on Rails. Bookmark the permalink.

Comments are closed.