php - Laravel 5.4 显示上传图片的问题

标签 php laravel laravel-5.3

我正在学习 Laravel 5.4,现在我正在尝试上传图像并在 html 中显示上传的图像。

看起来文件上传成功,但是当我想在 Html 中显示图像时,图像出现 404 错误,并且不显示任何内容。

这是我的设置和代码:

  public function store(Request $request)
    {
        $this->validate($request, [
            'name'  => 'required|string|max:255',
            'desc'  => 'required|string',
            'width' => 'required|numeric',
            'height'=> 'required|numeric',
            'artist'=> 'required|exists:artists,id',
            'photo' => 'required|file'
        ]);

        $artistInfo = Artist::find($request->artist);

        $fileExtension = $request->file('photo')->extension();

        $originalFileName = $request->file('photo')->getClientOriginalName();

        $filePath = Storage::putFile('public', $request->file('photo'), 'public');


        Photo::create([
            'name'      => $request->name,
            'origName'  => $originalFileName,
            'url'       => $filePath,
            'artist_id' => $request->artist,
            'desc'      => $request->desc,
            'width'     => $request->width,
            'height'    => $request->height
        ]);

        return $filePath;//this for test
    }

还有我的 Blade :

                @forelse($photos as $photo)
                    <tr>
                        <td>{{ $photo->name }}</td>
                        <td>
                            <img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>
                        <td>{{ $photo->artist->name . ' ' . $photo->artist->family }}</td>
                        <td>{{ $photo->desc }}</td>
                        <td>{{ $photo->width }}</td>
                        <td>{{ $photo->height }}</td>
                        <td class="text-center">
                            <a href="{{ route('artists.edit', ['id' => $photo->id]) }}">
                                <i class="material-icons">edit</i>
                            </a>
                        </td>
                        <td class="text-center">
                            <a href="{{ route('artists.edit', ['id' => $photo->id]) }}">
                                <i class="material-icons">assignment_turned_in</i>
                            </a>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td>There's no photo Yet</td>
                        <td>&mdash;</td>
                        <td>&mdash;</td>
                        <td>&mdash;</td>
                        <td>&mdash;</td>
                    </tr>
                @endforelse

注意:

我使用 php artisan storage:link 创建了一个指向我的公共(public)文件夹的符号链接(symbolic link),并且符号链接(symbolic link)工作正常...

我在 Blade 中更改了这一行:

<img class="img" src="{{ asset( 'public/storage/' . $photo->url) }}" alt="{{ $photo->name }}" /></td>

并尝试了这些方式:

 <img class="img" src="{{ asset(storage_path() . ($photo->url)) }}" alt="{{ $photo->name }}" /></td>


 <img class="img" src="{{ storage_path() . ($photo->url) }}" alt="{{ $photo->name }}" /></td>

$photo->url 具有图像文件名,例如 jDHPOTh7iejFABANSzasNbBWJ09DqLbqJb8ymJhv.png

请让我知道我的问题是哪一部分吗?

提前致谢

更新:

我正在使用 PhpStorm Ide,当我将公用文件夹下载到我的项目时 - 或同步此 - 我的符号链接(symbolic link)不会出现在公用文件夹列表中...

这是我的公共(public)文件夹中的ls:

list of folders in public

这是我的 phpStorm 项目 View 在我的主机中下载公共(public)文件夹的新副本后:

Folder list inside my IDE

作为注释: 我首先开始在 Windows 中开发这个网站,然后我将整个项目移动到我的 Linux 远程服务器...

最佳答案

对于任何寻找此问题答案的人,请点击此链接,其中包含很好的答案

https://stackoverflow.com/a/30191854/417899

关于php - Laravel 5.4 显示上传图片的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43583250/

相关文章:

php - 从 Analytics 中隐藏 Javascript 文件

php - Mysql IF 两列相等并且它们都大于一

javascript - 通过 AJAX 调用时,PHP 下载脚本不起作用

php - 如何删除 WordPress 中导航栏的这种 jquery 效果?

javascript - 如何将 Laravel URL::asset 与 javascript 变量一起使用

Laravel 迁移错误 : Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes laravel 5. 3

php - Laravel登录报错 'Header may not contain more than a single header, new line detected laravel login'

laravel - 将日期选择器值保存到 Laravel 中的数据库

laravel - 对 API 的请求在 Postman 中有效,但当我尝试使用 laravel/guzzle 时则无效

php - 在 laravel 5.4 中加入查询