【发布时间】: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>—</td>
<td>—</td>
<td>—</td>
<td>—</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->url 具有图像文件名,例如 jDHPOTh7iejFABANSzasNbBWJ09DqLbqJb8ymJhv.png
您能告诉我哪一部分是我的问题吗?
在此先感谢
更新:
我正在使用PhpStorm Ide,当我将公用文件夹下载到我的项目时 - 或同步这个 - 我的符号链接不会出现在公用文件夹列表中...
这是我的公共文件夹中的ls:
这是我的 phpStorm 项目视图之后在我的主机中下载公共文件夹的新副本:
作为注释: 我开始在 windows 中开发这个网站,然后我确实将整个项目移动到我的 linux 远程服务器...
【问题讨论】:
-
试过
{{ asset( 'public/') .'/'. $photo->url }}? -
@linktoahref 是的,但没用...!
-
我认为这可能会起作用,因为您尝试使用生成的 id
{{ storage_path($photo->url) }}获取路径 -
我之前试过... :(
标签: php laravel laravel-5.3