Last active
November 15, 2018 09:52
-
-
Save MohamedLamineAllal/0f9662099962c310936d67885a31f033 to your computer and use it in GitHub Desktop.
Laravel 5.7 how to serve files images privately
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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($filePath)){ // 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.($filePath))); // the response()->file() will add the necessary headers in our place | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function fileStorageServe($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 | |
// we check for the existing of the file | |
if (!Storage::disk('local')->exists($filePath)){ // note that disk()->exists() expect a relative path, from your the disk root path. so in our example we pass directly the path (/…/laravelProject/storage/app) is the default one (referenced with the helper storage_path('app') | |
abort('404'); // we redirect to 404 page if it doesn't exist | |
} | |
//file exist let serve it | |
// 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.($filePath))); // the response()->file() will add the necessary headers in our place (no headers are needed to be provided for images (it's done automatically) expected hearder is of form => ['Content-Type' => 'image/png']; | |
// big note here don't use Storage::url() // it's not working correctly. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<img src=""/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<img src=""/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment