0

When using PHP, any file manipulation is done relative to the server root, so something like mkdir("/home/website/public_html/a_directory would be used to create a directory in the public_html folder where the script is executed from.

In Rails, using the FileUtils module, it is relative to the Application's path like FileUtils.mkdir('public/a_directory') which will create a folder in the public folder of the application.

The problem I face is that from my Application, I would like to be able to create directories, move files, rename files/folders and remove file/folders relative to the server's root.

What's the best way to achieve this? OR am I missing something obvious?

Thanks, Stefan

Stefan Dunn
  • 5,363
  • 7
  • 48
  • 84

1 Answers1

1

You can use absolute paths in FileUtil:

FileUtil.mkdir('/tmp/foo')

will create the directory foo in then servers /tmp/ directory.

Rail.root holds the root of your rails application.

You can extend the path like Rails.root.join('public','a_directory').

Remember that the DOCUMENT ROOT is Rails.root.join('public')

Martin M
  • 8,430
  • 2
  • 35
  • 53
  • Maybe I didn't explain it properly. If I have my app hosted in `/home/website/public_html/my_app/`, I would like to be able to manipulate files / folders within `home/website/public_html/another_folder/` – Stefan Dunn May 28 '13 at 09:31
  • if your Rails app is in `/home/website`, than your application is in `/home/website/app` and your `DOCUMENT ROOT` is in `/home/website/public`. Then `Rails.root` points to `/home/website` and you can build paths starting there. – Martin M May 28 '13 at 09:36
  • Ah, now I think I understand your question, updating the answer – Martin M May 28 '13 at 09:39
  • I think I've just discovered what will work.. It was blindingly obvious (i think). I just need to use a `/` at the beginning of my `FileUtils.mkdir()` function to relate it to the server root. If I do not use the `/` it relates to the root of the application. – Stefan Dunn May 28 '13 at 09:40
  • Thanks for your help, I see where you were going with this answer. It will come in handy when switching between Staging and Production as I won't need to change my code to compensate. – Stefan Dunn May 28 '13 at 09:45