Skip to content

Instantly share code, notes, and snippets.

@placecodex
Created March 28, 2022 15:41
Show Gist options
  • Save placecodex/1c263a0ebf83d99f169d524a6cc5cc72 to your computer and use it in GitHub Desktop.
Save placecodex/1c263a0ebf83d99f169d524a6cc5cc72 to your computer and use it in GitHub Desktop.
protect files in laravel with router middleware
Route::get('/storage/uploads/{filename}', function ($filename)
{
$path = storage_path('app/public/uploads/images/' .'/'. $filename);
if (!File::exists($path)) {
$path = storage_path('app/public/uploads/' . 'NoImg.jpeg');
}
return Image::make($path)->response();
});
Route::get('tumblr/{filename}', function ($filename)
{
//return Storage::url('app/public');
$path = storage_path('app/public/uploads/tumblr/' . $filename);
if (!File::exists($path)) {
//abort(404);
$path = storage_path('app/public/uploads/' . 'NoImg.jpeg');
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
return Image::make($path)
->resize(320, 240)
->response()
->header("Content-Type", $type);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment