【问题标题】:Uploaded images not loading Laravel 7上传的图像未加载 Laravel 7
【发布时间】:2020-11-07 08:37:54
【问题描述】:

我在我的 laravel 站点中有一个功能,我将表单数据提交到数据库并将提交的图像保存在存储目录中。数据已正确存储在数据库中,但不知何故,图像无法加载,我不知道该怎么办了,因为这已经困扰了我好几天了。

如果有人能看到并告诉我我做错了什么,那就太好了!

我创建了一个符号链接,使用 php artisan link:storage

文件系统.php

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

带有存储功能的PostController.php:

public function store(Request $request)
{

    request()->validate([
        'title' => ['required', 'string', 'max:255',],
        'thumbnail' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048|unique:posts',
        'iframe' => ['required', 'string', 'max:255',],
    ]);

    $image = $request->file('thumbnail');
    $originalname = $image->getClientOriginalName();

    Storage::disk('public')->put($originalname, file_get_contents($image));


    Post::create([
        'title' => $request['title'],
        'thumbnail' => $originalname,
        'iframe' => $request['iframe'],
    ]);

    return redirect()->route('adminPost.index');
}

这就是我尝试加载图像的方式:

<img width="100px" src="{{asset('storage/public/'.$post->thumbnail)}}">

This is where my image is saved This is the path in the browser

【问题讨论】:

  • 你的存储路径是storage/app/public/'.$post->thumbnail

标签: php laravel file-upload laravel-7


【解决方案1】:

我发现了问题所在...不知何故我的符号链接无法正常工作。上传仅在我的 storage/app/public 中,而不是在我的 public/storage 文件夹中,我删除了 public/storage 文件夹并创建了一个新的符号链接。

删除目录:

rm public/storage

创建一个新的符号链接:

php artisan storage:link

【讨论】:

    【解决方案2】:

    您是否在表单元素上添加了 enctype 属性?

    如果不添加以下 ===> enctype="multipart/form-data"

    【讨论】:

      【解决方案3】:

      我认为问题在于 html 中的 <image> 标签。

      <img src="{{ Storage::url("/storage/app/{$post->thumbnail}") }}" alt="{{ $post->filename }}" />
      

      有关 Retrieving Files from storage 的更多详细信息,请参阅 laravel 文档

      【讨论】:

        猜你喜欢
        • 2012-02-17
        • 1970-01-01
        • 2020-04-21
        • 1970-01-01
        • 2020-08-22
        • 1970-01-01
        • 2019-10-23
        • 2019-11-10
        • 1970-01-01
        相关资源
        最近更新 更多