【发布时间】:2017-03-23 16:06:10
【问题描述】:
嗨,我完全被 Laravel 文件系统吓坏了,我在网上搜索了三天,但找不到从“storage/app/public”文件夹中显示图像或将图像存储在另一个文件夹中的图像“存储/应用/公共”之外的文件夹。 我在带有 Laravel 5.3 的 Windows 上使用 Homestead。这是上传图像并将路径保存到数据库中的“存储控制器”。
class ClassName extends Controller
{
public function store(Request $request)
{
$today = Carbon::today(); //Today
$year = $today->year; // year int(2016)
$month = $today->month; // month int(11)
$day = $today->day; // day int(5)
//make year/month/day directory if not available then upload images to these directories sent from the form
$path = $request->file('ProductImage')->store(public_path().'/'.$year.'/'.$month.'/'.$day);
if ($request->file('ProductImageOne')) {
$pathone = $request->file('ProductImageOne')->store('public'.'/'.$year.'/'.$month.'/'.$day);
}
else{
$pathone = NULL;
}
if ($request->file('ProductImageTwo')) {
$pathtwo = $request->file('ProductImageTwo')->store('public'.'/'.$year.'/'.$month.'/'.$day);
}
else{
$pathtwo = NULL;
}
//store data's sent by the form
$product = new Product;
$product->ProductImage = $path;
$product->ProductImageOne = $pathone;
$product->ProductImageTwo = $pathtwo;
$product->save();
Session::flash('flash_message', 'Your Image is uploaded.');
return redirect('/products/create');
}
}
到目前为止,我没有任何问题,图像已上传到:
\storage\app\public\2016\11\8\image-name.png
但是当我试图在视图文件中显示图像时,它不会显示出来。这是我的控制器方法,它发送视图的路径:
public function edit($id)
{
//Get product from $id to edit
$product = Product::findOrFail($id);
//send the data above and load the edit product view (edit form)
return view('pages.edit_product')->with('product', $product);
}
这是我要显示图像的视图代码:
<img src="{{ asset($product->ProductImage) }}" width="45">
返回:
<img src="http://laravel.dev:8000/public/2016/11/6/0b7e4e3dbf6907fa1b858fa5cd0bf3ce.jpeg" width="45">
http://laravel.dev:8000/public/2016/11/6/0b7e4e3dbf6907fa1b858fa5cd0bf3ce.jpeg
当我想直接转到该图片的链接时,它会说:
抱歉,找不到您要查找的页面。
RouteCollection.php 第 161 行中的 1/1 NotFoundHttpException:
我什至通过“php artisan storage:link”做了“符号链接”,我对此知之甚少。它会在公用文件夹中生成一个空白的“.storage”文件。 这是我与 Laravel 的第一个项目,我真的很喜欢它,直到我遇到这个问题,上传图像并将其展示给观众,现在我很失望。请帮忙
【问题讨论】:
-
您是否创建了从 /public 文件夹到 /storage/app/public 文件夹的符号链接?
-
是的,我做到了,并且在“laravel/public/”文件夹中有一个无法编辑的空白存储文件。
标签: php laravel symlink laravel-5.3