Search code examples
phplaravelfile-upload

How to upload file to another directory Laravel


I want to store file to another directory. how i configure the filesystems config to point to another directory?

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

Directory Structure example: enter image description here

My laravel app is on C directory. I want to store the file on Z directory.


Solution

  • On filesystems.php add to disk array:

    'disks' => [
        'z' => [
            'driver' => 'local',
            'root' => 'Z:/',
        ],
        // ...
    ],
    
    

    Then call the storage facade like:

    use Illuminate\Support\Facades\Storage;
    
    // ...
    
    $path = Storage::disk('z')->put('example.txt', 'Contents');
    

    Note: As mentioned by @Techno you may need to set the appropriate permissions on the target directory to allow Laravel to write to it. More info on docs.