0
votes

I am trying to save a file in a folder outside root directory and then read it's contents.

For example My working directory is /var/www/html/project_folder and I want to save file in /var/www/new_folder

I'm working with laravel 5.2, so my root path is /var/www/html/project_folder/public

I have granted all permissions to new_folder. I have tried $_SERVER['DOCUMENT_ROOT'] or realpath, but not working.

1
Why do you want to access a file from outside your root directory? That is an added protection to your webserver. Still you can use it with readfile(). (Not sure about your requirement)Thamilhan

1 Answers

2
votes

Go to config/filesystems.php and add your own custom storage path:

return [

    'default' => 'custom',
    'cloud' => 's3',
    'disks' => [

        'local' => [
            'driver' => 'local',
            'root'   => storage_path().'/app',
        ],

        'custom' => [
            'driver' => 'custom',
            'root'   => '../path/to/your/new/storage/folder',
        ],

        's3' => [
            'driver' => 's3',
            'key'    => 'your-key',
            'secret' => 'your-secret',
            'region' => 'your-region',
            'bucket' => 'your-bucket',
        ],

        'rackspace' => [
            'driver'    => 'rackspace',
            'username'  => 'your-username',
            'key'       => 'your-key',
            'container' => 'your-container',
            'endpoint'  => 'https://identity.api.rackspacecloud.com/v2.0/',
            'region'    => 'IAD',
        ],

    ],
];

Remember to clear cache after changes:

php artisan config:cache

You have more information here: https://laravel.com/docs/5.0/filesystem