简体   繁体   中英

How to access file from folder outside root directory in PHP

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM