Laravel文件上传NotFoundHttpException

I'm using Laravel to upload an image to my folder.

$file = Input::file('largeImage');
$filePath = '/uploads/'.date("Y/m").'/'.time().'/';
$path = $filePath;
$file->move($path, $file->getClientOriginalName());

The image is successfully uploaded.

Now when I try to access it:

http://localhost:8080/uploads/2015/01/1420644761/10377625_673554946025652_6686347512849117388_n.jpg

I'm having the Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException error.

I even tried http://localhost:8080/public/uploads/2015/01/1420644761/10377625_673554946025652_6686347512849117388_n.jpg

But the same error. What might be the problem? I checked the uploads folder and the image is there.

You need to add a call to public_path helper:

$filePath = public_path() . '/uploads/'.date("Y/m").'/'.time().'/';

Otherwise it will place it in the app root, I think.

Ok, so it IS a Routing Issue. To solve this particular one, define a Route::get that navigates to your file.

Route::get("/download/{year}/{month}/{time}/{filename}", "Controller@downloadFile");

Then you'll need a function in that controller that handles the file download:

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');
}

Which in theory should work for your needs. The path may need to be modified to fit your needs but this should be the general idea. Test that out and let me know if it works.

Note

This isn't the best way to handle downloads, as you need to know the exact filename of the file you want, but it points you in the right direction.

This worked for me:

    $file = Input::file('largeImage');
    $filePath = '/uploads/'.date("Y/m").'/'.time().'/';
    $filename = $file->getClientOriginalName();
    $path = public_path().$filePath;
    $file->move($path, $file->getClientOriginalName());