Search code examples
ruby-on-railsdirectoryfileutils

Ruby on Rails: FileUtils relative to server root?


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


Solution

  • 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')