简体   繁体   English

上传的图片未加载 Laravel 7

[英]Uploaded images not loading Laravel 7

I have a function in my laravel site where i submit form data to a database and save the submitted image in the storage directory.我在我的 laravel 站点中有一个 function,我将表单数据提交到数据库并将提交的图像保存在存储目录中。 The data is stored correctly in the database, but somehow the images wont load and i dont know what to do anymore since this has been bugging me for a couple of days now.数据已正确存储在数据库中,但不知何故图像无法加载,我不知道该怎么做,因为这已经困扰了我好几天了。

If someone can see and tell me what i did wrong, that would be great!如果有人能看到并告诉我我做错了什么,那就太好了!

I've created a symlink using php artisan link:storage我使用php artisan link:storage创建了一个符号链接

filesystems.php文件系统.php

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

PostController.php with the store function: PostController.php 与商店 function:

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');
}

This is how i am trying to load the image:这就是我尝试加载图像的方式:

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

This is where my image is saved This is the path in the browser这是保存我的图像的位置这是浏览器中的路径

I found out what the problem was... Somehow my symlink didn't work correctly.我发现了问题所在......不知何故我的符号链接无法正常工作。 The uploads were only in my storage/app/public and not in my public/storage folder, i deleted the public/storage folder and made a new symlink.上传仅在我的 storage/app/public 中,而不是在我的 public/storage 文件夹中,我删除了 public/storage 文件夹并创建了一个新的符号链接。

Deleting the directory:删除目录:

rm public/storage

Making a new symlink:创建一个新的符号链接:

php artisan storage:link

Did you add the enctype attribute on your form element?您是否在表单元素上添加了 enctype 属性?

if not add the following ===> enctype="multipart/form-data"如果不添加以下 ===> enctype="multipart/form-data"

I think the problem is the <image> tag in html.我认为问题在于 html 中的<image>标签。

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

Refer to the laravel documentation for more details on Retrieving Files from storage有关从存储中检索文件的更多详细信息,请参阅 laravel 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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