在我的web应用程序中,我用这种方法保存用户头像:
$avatarPath = request()->file('avatar')->store('avatars');
然后我在数据库中存储$avatarPath。要在页面上显示化身,我使用:
<img class="doc-avatar" src="{{ Storage::disk('local')->url($doctor->image) }}" />
但正如你猜的那样,不会有任何图像,我也不知道为什么。
我创建了一个符号链接:
php artisan storage:link
还检查了storage/app/public/avatars中的avatars文件夹,我的图像在那里。
我可以得到所有关于图像的元数据
Storage::url($doctor->image);
或者
Storage::size($doctor->image);
但是如果我在浏览器中复制返回的网址,就会出现404错误。
http://localhost:8080/storage/avatars/04it83vKYnobT7l8dNXwt1pE0VxiT9L5lQPQ6A1Z.jpeg
http://localhost/storage/avatars/04it83vKYnobT7l8dNXwt1pE0VxiT9L5lQPQ6A1Z.jpeg
两者都返回404。
这是我的配置/文件系统。php
'default' => env('FILESYSTEM_DRIVER', 'public'),
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
上传将把图像添加到存储/app/public/avatars
,同时再次检查文件是否同步到公共/avatars
。公用文件夹下的文件是唯一可访问的文件,而不是存储/app/public/avatars
下的文件。