简体   繁体   中英

Image doesn't save in public folder laravel

I'm learning laravel but I have problem now with the image, I want it to save it in storage/app/public/avatar so I can show it, but it keep saving it in storage/app/avatar

I write this in the.env

FILESYSTEM_DRIVER=public

And then I generate the storage link

php artisan storage:link 

but still the same, every time I upload a photo it saving it in storage/app/avatar

Anyone can help me?

In filesystems.php

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

Here:

 $attributes['avatar'] = request('avatar')->store('public/avatars');

In USer.php

public function getAvatarAttribute($value)
{
    return asset($value);
}

this method was working correctly when i was using a random avatar link, but now it show nothing, i check my database and the link is there and it's right

public function getAvatarAttribute($value)
 {
    asset('storage/avatars/'.$image->name);
 }

$attributes['avatar'] = request('avatar')->store('public/avatars');

In the controller, update method:

    $user->update($attributes);
    $user->password = Hash::make($user['password']);
    $user->save();
    return redirect($user->path());

In the show.blade

 <img
            src= "{{ $user->avatar }}"
            alt=""
            class="rounded-full mr-4 absolute"
            style="width: 150px; left: calc(50% - 75px); top: 300px"
        >

but the src become "(unknown)"

This looks more like a configuration cache issue as your filesystem looks properly configured.

The storage/app folder is the path of laravel's default disk (the local disk). Since you had specified to use public as the default disk in.env but your application is still using the local disk path, it looks like a cache issue

Try using

php artisan config:clear

And then attempt

request('avatar')->store('avatars');

If it was indeed a cache issue, then the file should be stored in the storage/app/public folder.

To get the full path to the image, use

Storage::url('avatar/'.$user->avatar);

So in the accessor, use

public function getAvatarAttribute($value)
{
    return Storage::url('avatar/'.$value);
}

Using asset() to get the path to the file is not recommended because if some day you wish to switch storage to the cloud (S3, or any other storage. basically, you want to not use the storage folder of your application), then the asset() will no longer give you the correct path and you'd need to change it everywhere. But when using the storage facade, all you need to do is change it in the filesystems.php

Also, just to test it yourself, you could also specify the disk when storing by specifying the disk as the second argument to the store method

request('avatar')->store('avatars', 'public');
Storage::disk('public')->url('avatar/'.$user->avatar);

The store method stores the files in storage/app directory. That's why your code is storing the image in storage/app/ avatars

request('avatar')->store('avatars');

This will place it under storage/app/public/avatars

 request('avatar')->store('public/avatars');

To show it

asset('storage/avatars/`.$image->name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM