strange File.open and Dir.mkdir issue

I'm devloping an application that allows users to attach files to their profiles. At the moment they can only attch one file per profile. We have some unique requirements so we have to write our own code for this (can't use act_as_attachment or file_column).

Anyhoo, we've got pretty much everything working except one really bizzare problem. We want to save the attachments into a directory structure like this

    public/attachments/<user_id>/<profile_id>

which would end up being something like

    public/attachments/23/18

the only problem is that when we try to either open the file for writting with something like

      File.open(@save_location + '/' + @file_name, "wb") do |f|         f.write(@temp_file.read)       end

we get

   No such file or directory - public/attachments/23/18/test-file.jpg

or try and make a new directory using

    Dir.mkdir(@save_location) unless File.directory?(@save_location)

we get the folowing error:

   No such file or directory - public/attachments/23/18

or something similar depending on the user_id and profile_id of course. This is happening on my development machine and I have to admint I haven't tried it on another (don't have another set up or available at this time) but I can't seem to work out why it won't create the directories. If I try it with just say

      File.open('public/attachments/' + @file_name, "wb") do |f|         f.write(@temp_file.read)       end

it works fine however in this case both 'public' and 'attachments' already exist so I'm guessing it's got something to do with creating the new directories.

Any ideas? Could it have something to do with the public directory being under Subversion (although the attachments directory is not).

Thanks in advance Dale

Are you trying to create the directory structure 23/18/ in one fell swoop? Because the standard Dir.mkdir will only make a single directory at a time.

If you want to create all of the directories necessary for your path in one go, you can use FileUtils.mkdir_p:

which is equivalent to the 'mkdir -p' command at the shell.

Chris

Chris,

Thank you so much for pointing this out, that certainly saved the day.

Dale