I am new to Laravel and trying to store private images so that only authenticated users can access them. Firstly I stored images in Public/UserImages folder. But here all the pictures are accessible to unauthenticated users as well by going to Inspect Element of chrome and then changing the user IDs. Kindly help out me...
相关问题
- Laravel Option Select - Default Issue
- Laravel 5.1 MethodNotAllowedHttpException on store
- Laravel - Implicit route model binding with soft d
- laravel : php artisan suddenly stop working
- How to execute MYSQL query in laravel?
相关文章
- laravel create model from custom stub when using p
- send redirect and setting cookie, using laravel 5
- How to send parameters to queues?
- Bcrypt vs Hash in laravel
- Laravel: What's the advantage of using the ass
- How to make public folder as root in Laravel?
- Input file in laravel 5.2?
- What is the difference between Session::set and Se
I got the same issue some days ago and came up with this solution:
First thing you have to do is upload the file to a non-public directory. My app is storing scanned invoices, so I'm going to place them inside
storage/app/invoices
. The code for uploading the file and generating the url would be:The url returned should result in something like http://yourdomain.com/storage/invoices/uniquefilename.jpg
Now you have to create a controller that uses the
auth middleware
to ensure the user is authenticated. Then, define a method that grabs the file from the private directory and returns it as a file response. That would be:The last thing is register the route inside your
routes/web.php
file:So there you have it, a pretty reusable snippet for all your projects that deals with private files :)
It's really up to you. It'll need to be outside the
public
directory - I'd personally pickresources/uploads
orstorage/uploads
, or store them off-server entirely using the cloud filesystem support.Whatever you pick, you'll need a route that fetches the file and passes it along to the user after first checking that they have access.
Here how to do it in Laravel 5.7
To have private files (images), you need to serve the files through a route => controller flow. And your auth middleware will handle authentication, and permission. If further authorization are needed you handle it in the controller.
So first we set a route:
Here we can have one route that handle all our files [i personally don't prefer that]. We can do that using such a route (it's like a wildcard).
You can name it too like that:
Otherwise we create a route for each type/category of files: (advantage: you will be able to control better the accessibility. (Each route and type of resources and it's rules. If you want to achieve that with the wild card route (let me call it that) you need to have conditional blocks (if else, handling all different situations. It's unnecessary operations [going directly to the right block, when the routes are separate is better, plus out of that, it allow you to organize better the permissions handling]).
We had our routes set NOW Controller/Controllers
The wild card one
The per route one
(big difference, is the parameter, now it reference just the file name, and not the relative path to storage disk root)
Now you can check by forming the correct url (go to storage copy past the file name, and form your route. it should show you the image)
One last thing left:
How to show that in view
the wild card one
Note that
routeName
here in the example above will bestorage.file
, andfileParam
would befilePath
. $storageRelativePath for example you get from the db (generally that's what it will be).The per route
Same but we provide only the filename.
Notes: The best way to send such a response, is using response()->file();. That what you will find in the 5.7 doc. That performance wise against Image::make($storagePath)->response();. Unless you need to modify it in the fly.
You can check my article in medium: https://medium.com/@allalmohamedlamine/how-to-serve-images-and-files-privatly-in-laravel-5-7-a4b469f0f706
Following is how I solved the problem of storing images in Laravel 5 such that only authenticated users can view the images. People who are not authenticated will be directed to a login page. My server is a Ubuntu/Apache2 server.
Create the directory /var/www/YOURWEBSITE/app/Assets/Images
Add route to app/Http/routes.php.
Route::get('/images/{file}','ImageController@getImage');
Create a controller app/Http/Controllers/ImageController.php
In your view you have img tags which have:
This of course assumes test.jpg is a file in /var/www/YOURWEBSITE/app/Assets/Images/
You can of course add more logic such as not hardcoding the path of the images, etc. This is just a simple example to enforce authentication. Note the use of middleware('auth') in the controller constructor.