Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel upload a file to different storage outside the project directory

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?

like image 853
eugenebermudez Avatar asked Jul 23 '19 03:07

eugenebermudez


People also ask

How do I change storage location in Laravel?

In Laravel 5 it this works by using $app->useStoragePath('/path/') in bootstrap/app. php .

How do I unlink a folder in Laravel?

You could use PHP's unlink() method just as @Khan suggested. But if you want to do it the Laravel way, use the File::delete() method instead. $files = array($file1, $file2); File::delete($files);

Where should I store files in Laravel?

When using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory.


1 Answers

You can add multiple disk in your file system.

Just locate on same website storage folder

Example:

Project 1:

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

Project 2:

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

the path should be the 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);
like image 139
Emtiaz Zahid Avatar answered Oct 22 '22 05:10

Emtiaz Zahid