Skip to content

Instantly share code, notes, and snippets.

@MohamedLamineAllal
Last active December 17, 2018 13:23
Show Gist options
  • Save MohamedLamineAllal/435d4378d1c22e534f34f1986ada9d11 to your computer and use it in GitHub Desktop.
Save MohamedLamineAllal/435d4378d1c22e534f34f1986ada9d11 to your computer and use it in GitHub Desktop.
How to serve images and files privatly in Laravel 5.7 (Medium)
<?php
public function getCompaniesLogo($file) {
// know you can have a mapping so you dont keep the sme names as in local (you can not precsise the same structor as the storage, you can do anything)
// any permission handling or anything else
$filePath = config('fs.gallery').DIRECTORY_SEPARATOR.$file; // here in place of just using 'gallery', i'm setting it in a config file
// here i'm getting only the path from the root (this way we can change the root later) / also we can change the structor on the store itself, change in one place config.fs.
// $filePath = Storage::url($filePath); <== this doesn't work don't use
// check for existance
if (!Storage::disk('local')->exists($file)){ // as mentionned precise relatively to storage disk root (this one work well not like Storage:url()
abort('404');
}
// if there is parameters [you can change the files, depending on them. ex serving different content to different regions, or to mobile and desktop …etc] // repetitive things can be handled through helpers [make helpers]
return response()->file(storage_path('app'.DIRECTORY_SEPARATOR.($file))); // the response()->file() will add the necessary headers in our place
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment