【问题标题】:Laravel 5.4 show uploaded image issueLaravel 5.4 显示上传的图片问题
【发布时间】:2017-09-20 20:23:03
【问题描述】:

我正在学习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
    }

还有我的刀:

                @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 为我的公用文件夹创建了符号链接,并且符号链接工作正常...

我在刀片中更改了这一行:

<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-&gt;url 具有图像文件名,例如 jDHPOTh7iejFABANSzasNbBWJ09DqLbqJb8ymJhv.png

您能告诉我哪一部分是我的问题吗?

在此先感谢

更新:

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

这是我的公共文件夹中的ls

这是我的 phpStorm 项目视图之后在我的主机中下载公共文件夹的新副本:

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

【问题讨论】:

  • 试过{{ asset( 'public/') .'/'. $photo-&gt;url }} ?
  • @linktoahref 是的,但没用...!
  • 我认为这可能会起作用,因为您尝试使用生成的 id {{ storage_path($photo-&gt;url) }} 获取路径
  • 我之前试过... :(

标签: php laravel laravel-5.3


【解决方案1】:

我认为问题出在这里:

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

您将文件放在public 文件夹和此处:

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

您正试图从public/storage/ 获取它。所以改变路径:

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

然后再试一次。

注意:这里上传的图片路径保存在public/image.extension,所以从同一个public目录中获取

【讨论】:

  • 感谢您的回答,但您的建议是什么?我尝试了很多不同的方式......
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    感谢大家的参与,但就我而言,有一个棘手的问题......

    我使用root 登录到我的服务器,当我输入php artisan storage:link 时,符号链接是由root 作为owner 创建的,看来Apache 没有访问此符号链接的权限因此,http 中的文件无法访问。

    解决方案

    我有一个服务于 5 个不同网站的 vd,所以 我只是将符号链接所有者的所有者更改为我的相对用户名,问题就解决了......

    chown -R usergroup:username /path/to/link
    

    就是这样……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 2022-06-21
      • 2017-06-23
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多