我正在学习 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>—</td>
<td>—</td>
<td>—</td>
<td>—</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
:
这是我的 phpStorm 项目 View 在我的主机中下载公共(public)文件夹的新副本后:
作为注释: 我首先开始在 Windows 中开发这个网站,然后我将整个项目移动到我的 Linux 远程服务器...
最佳答案
对于任何寻找此问题答案的人,请点击此链接,其中包含很好的答案
关于php - Laravel 5.4 显示上传图片的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43583250/