Code for uploading files to the filesystem instead of db

Does anyone have any standard code for uploading files to the
filesystem?

I’ve got code already for uploading images to the database but I’d
prefer to store the files on the filesystem.

In particular I need to know how to

(a) ensure a file has been selected in the file_field
(b) how to extract the filename from the file_field
© how to actually move the file from client to server

–John

Check out acts as attachment.

http://svn.techno-weenie.net/projects/plugins/acts_as_attachment/

Problem is this requires RMagick

Only to resize images.

Josh P. wrote:

Check out acts as attachment.

http://svn.techno-weenie.net/projects/plugins/acts_as_attachment/

Problem is this requires RMagick - I can’t seem to get this to work on
my machine - don’t know why.
My production server arrives tomorrow so I can try again with that.

Don’t suppose anyone has a solution that doesn’t need any other plugins?

–John

For anyone who is interested, I found this really simple way of
uploading a file to the filesystem without the need for any plugins or
any complicated code. Takes 10 minutes to get it going.

http://www.forthecode.com/user/comment/15

–John

jh100000 wrote:

Does anyone have any standard code for uploading files to the
filesystem?

I’ve got code already for uploading images to the database but I’d
prefer to store the files on the filesystem.

In particular I need to know how to

(a) ensure a file has been selected in the file_field
(b) how to extract the filename from the file_field
© how to actually move the file from client to server

–John

Not too hard

#view
<%= form_tag({:action => ‘upload’}, {:multipart => true}) %>
<%= file_field_tag ‘my_file’ %>
<%= end_form_tag %>

#controller
def upload
file = params[:my_file]
if file.size > 0
File.open("#{RAILS_ROOT}/some/dir/#{file.original_filename}",
‘w+’) do |f|
f.write(file.read)
end
end
end

a) “file.size > 0” tests whether or not there a file was actually
uploaded
b) “file.original_filename” retrieves the name of the file
c) the directory location provided to File.open is where the uploaded
file will be written.

Just make sure permissions are set properly in the directory you are
uploading to and that should be all you need.