Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel putFileAs path error

Tags:

php

laravel

I use the putFileAs method on the Storage facade to upload my photos.

        $gallery = $request->file('feature_image');
        Storage::putFileAs(
            'just_for_test_putfileas', $gallery, time().'.'. $request->file('feature_image')->getClientOriginalExtension()
        );

it works great and default putFileAs dir is "storage/app" and will put my file in this direction "storage/app/just_for_test_putfileas"

when I use ../ in dir to put the file in some other direction it cause an error "whoops, it seems there is something wrong" this block of code cause error

            $gallery = $request->file('feature_image');
            Storage::putFileAs(
              '../just_for_test_putfileas', $gallery, time().'.'. $request->file('feature_image')->getClientOriginalExtension()
        );
like image 610
movAhed Avatar asked Mar 17 '26 07:03

movAhed


1 Answers

When you use the Storage facade without specifying a disk, it will use the default disk which is usually the local disk. This disk is rooted/jailed to the /storage/app/ directory and you cannot escape it.

See your settings in config/filesystem.php:

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

If you want to be able to read and write files using Laravel's filesystem functionality to a different folder, you can create a new disk and jail it to another location:

'disks' => [
    'my-disk' => [
        'driver' => 'local',
        'root' => storage_path(),
    ],

In my example above I have created a new disk jailed to the /storage/ path, which is the path above /storage/app/. You can then use your disk with the Storage facade like this:

Storage::disk('my-disk')->putFileAs(...);
like image 53
Jonathon Avatar answered Mar 18 '26 20:03

Jonathon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!