首页 文章

Laravel:访问公共文件夹中的图像

提问于
浏览
0

嗨,我完全被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的第一个项目,我真的很喜欢它,直到我遇到这个问题上传图像并将其显示给 Spectator ,现在我很失望 . 请帮忙

1 回答

  • 0

    您需要从图片网址中删除 public ,以便您拥有以下内容:

    "http://laravel.dev:8000/2016/11/6/0b7e4e3dbf6907fa1b858fa5cd0bf3ce.jpeg"
    

    所以将 store('public'.'/'.$year.'/'.$month.'/'.$day); 更改为 store($year.'/'.$month.'/'.$day);

相关问题