我正在使用Laravel将图像上传到我的文件夹。
$file = Input::file('largeImage');
$filePath = '/uploads/'.date("Y/m").'/'.time().'/';
$path = $filePath;
$file->move($path, $file->getClientOriginalName());
图像已成功上传。
现在,当我尝试访问它时:
http://localhost:8080/uploads/2015/01/1420644761/10377625_673554946025652_6686347512849117388_n.jpg
我遇到了Symfony ' Component ' HttpKernel ' Exception ' NotFoundHttpException
错误。
我什至尝试了http://localhost:8080/public/uploads/2015/01/1420644761/10377625_673554946025652_6686347512849117388_n.jpg
但同样的错误。可能有什么问题?我检查了上传文件夹,图像在那里。
您需要
添加对public_path帮助程序的调用:
$filePath = public_path() . '/uploads/'.date("Y/m").'/'.time().'/';
否则,我认为它会将其放置在应用程序根目录中。
这对我有用:
$file = Input::file('largeImage');
$filePath = '/uploads/'.date("Y/m").'/'.time().'/';
$filename = $file->getClientOriginalName();
$path = public_path().$filePath;
$file->move($path, $file->getClientOriginalName());
好的,所以这是一个路由问题。要解决此特定问题,请定义导航到文件Route::get
。
Route::get("/download/{year}/{month}/{time}/{filename}", "Controller@downloadFile");
然后,您需要在该控制器中处理文件下载的函数:
public function downloadFile($year, $month, $time, $filename){
$path = public_path()."/uploads/".$year."/".$month."/".$time."/".$filename".jpg";
// Should equate to: "/uploads/2015/01/142064476110377625_673554946025652_6686347512849117388_n.jpg
return Response::download($path, 'image.jpg');
}
理论上应该适合您的需求。可能需要修改路径以满足您的需求,但这应该是一般想法。测试一下,让我知道它是否有效。
注意
这不是处理下载的最佳方式,因为您需要知道所需文件的确切文件名,但它为您指明了正确的方向。