Laravel upload a file to different storage outside the project directory

为君一笑 提交于 2020-12-05 07:19:59

问题


I'm creating a CMS wherein I can upload files(photos, pdf, etc) using Laravel's file upload. What I'm doing differently is that I want to store the file outside my CMS project directory let's say my website's storage folder. BTW I'm creating two different projects

Laravel documentation says that I can change the path where it will be uploaded in the filesystems.php what I did is get the relative path of my website storage folder and paste it here(see below).

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

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ], 

As expected it's not working. Any help guys?


回答1:


You can add multiple disk in your file system.

Just locate on same website storage folder

Example:

Project 1:

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

Project 2:

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

the path should be same location.

../path/to/your/new/storage/folder

and then you can use it like:

Storage::disk('custom_folder_1')->put('filename1', $file_content);
Storage::disk('custom_folder_2')->put('filename2', $file_content);


来源:https://stackoverflow.com/questions/57156204/laravel-upload-a-file-to-different-storage-outside-the-project-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!