michalis's avatar

request()->file()->store() does not save in public directory

it saves into the storage/app directory, so when I create a symlink things dont work, what am I missing?

shouldnt it save in the storage/app/public directory?

0 likes
23 replies
munazzil's avatar

Have you try with below code,

  php artisan storage:link
michalis's avatar

yes i did that but it create a symink from public/storage to storage/app/public

but the files are being saved in storage/app which according to the docs should be saving in storage/app/public

yelinnoo's avatar

@michalis instead of writing FILESYSTEM_DRIVER=public, you need to change FILESYSTEM_DISK=public.

1 like
NoLAstNamE's avatar

@yelinnoo You are replying to a 4-year-old comment, the older versions of Laravel use FILESYSTEM_DRIVER. I think you're using newer versions.

munazzil's avatar

It will create the correct storage path on your system and then you can access by below.

<img  href="{{asset('public/yourimage.jpg')}}"  />
munazzil's avatar

Can you show your save function in your controller and your index.blade.php?

michalis's avatar

@MUNAZZIL -

public function store($id)
    {
        // dd(request()->all());

        $path = request()->file('asset')->store('public');

        $asset = new Asset();
        $asset->project_id = $id;
        $asset->title = request('title');
        $asset->file = $path;
        $asset->downloaded = 0;
        $asset->save();

        return back();
    }

as you see i have to tell laravel to store in public folder or else it doesnt

            @foreach ($project->assets as $asset)
                <li>{{ $asset->title }}, <a href="{{ asset($asset->file) }}">download</a>, Downloaded {{ $asset->downloaded }} time(s)</li>
            @endforeach

and the url i get is this, which is wrong

http://127.0.0.1:8000/public/kMpIp6i0xeUMLfbweoI2nBjQ40sOX18bWswW66Hy.jpeg

edoc's avatar

this should work if the file is saved in storage/app/public

 <a href="{{ asset('storage/'.$asset->file) }}">

check your filesystem config as well

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

@michalis

helpmyworld's avatar

@edoc how are you Edoc?

I don't want to save files in storage/app/public.

I want to save them in my public/images

Please help me improve this code

public function store(Request $request)
    {
        $new_book = new \App\Book;
        $new_book->title = $request->get('title');
        $new_book->description = $request->get('description');
        $new_book->author = $request->get('author');
        $new_book->publisher = $request->get('publisher');
        $new_book->price = $request->get('price');
        $new_book->stock = $request->get('stock');
        $cover = $request->file('cover');
        if($cover){
            $cover_path = $cover->store('book-covers', 'public');
            $new_book->cover = $cover_path;
        }
        $new_book->slug = str_slug($request->get('title'));
        $new_book->created_by = \Auth::user()->id;
        $new_book->save();
        $new_book->categories()->attach($request->get('categories'));
        if($request->get('save_action') == 'PUBLISH'){
            return redirect()
                ->route('admin.books.create')
                ->with('status', 'Book successfully saved and published');
        } else {
            return redirect()
                ->route('admin.books.create')
                ->with('status', 'Book saved as draft');
        }
    }

i want to display them like so

src={{asset('/images/' . $book->image)}}"
``

michalis's avatar

@EDOC - filesystem config looks the same as yours, i did not mess with it

this is my code

$path = request()->file('asset')->store('assets');

this should save in storage/app/public/assets

but it saves in storage/app/assets

for the life of me I dont know why

edoc's avatar

can you try this @michalis

//public disk
$path = request()->file('asset')->store('assets', 'public');
4 likes
michalis's avatar

@EDOC - ok this worked and I dont understand why it worked, did i not read the documentation properly?

edoc's avatar
edoc
Best Answer
Level 24

i guess you forgot to set the default file driver so just add this to your .env

FILESYSTEM_DRIVER=public
8 likes
michalis's avatar

@EDOC - this wasnt in the documentation either I think, it just an overwhelming feeling trying to learn laravel when you cant trust the docs

michalis's avatar

@EDOC - @edoc

I apologize if i'm being persistent, but I cant find anything in the docs about setting the filesystem driver, could you possibly provide a link? trying to understand whats going on

1 like
edoc's avatar

when you check config/filesystem you can see this piece of code

    'default' => env('FILESYSTEM_DRIVER', 'local'),

this sets the default driver from the environmental variable FILESYSTEM_DRIVER.

In your case, you set FILESYSTEM_DRIVER=public in .env right? so your default driver is set to public. And when you store images Laravel uses the config for the public disk from config/filesystem

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

@michalis

pushpak's avatar

Even If code in the config/filesystem is 'default' => env('FILESYSTEM_DRIVER', 'public'),

And you set FILESYSTEM_DRIVER=local in .env

Files still gets stored to 'local' So change the FILESYSTEM_DRIVER=public in .env

or

delete the FILESYSTEM_DRIVER=local in .env and set 'default' => env('FILESYSTEM_DRIVER', 'public'), in config/filesystem

maxghis's avatar

For my case you go on public folder and you delete storage folder afert it you do php artisan storage:link

1 like
sebaxba's avatar

Hi, to solve this issue just change FILESYSTEM_DISK=local into FILESYSTEM_DISK=public in your .env file.

1 like

Please or to participate in this conversation.