# Strange File.open and Dir.mkdir issue

**URL:** <https://www.ruby-forum.com/t/strange-file-open-and-dir-mkdir-issue/83336>\
**Category:** Rails\
**Created:** [January 25, 2007, 10:21am UTC](https://www.ruby-forum.com/t/strange-file-open-and-dir-mkdir-issue/83336 "2007-01-25T10:21:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [January 25, 2007, 10:21am UTC](https://www.ruby-forum.com/t/strange-file-open-and-dir-mkdir-issue/83336/1 "2007-01-25T10:21:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [January 25, 2007, 10:34am UTC](https://www.ruby-forum.com/t/strange-file-open-and-dir-mkdir-issue/83336/2 "2007-01-25T10:34:09Z")

</div>

On Jan 25, 9:20 am, “PeteSalty” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
> 
> ```

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:

[http://www.ruby-doc.org/stdlib/libdoc/fileutils/rdoc/classes/FileUtils.html#M000528](http://www.ruby-doc.org/stdlib/libdoc/fileutils/rdoc/classes/FileUtils.html#M000528)

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

Chris

---

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [January 25, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/strange-file-open-and-dir-mkdir-issue/83336/3 "2007-01-25T21:31:05Z")

</div>

Chris,

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

Dale
