How to store photos in a publicly inaccessible place and generate download links for them dynamically?
Let's say I'm building a simple photo gallery application. Admins can upload hi-res photos to the application. These photos can later be requested for download by users. The thing is I don't want the photos to reside in a location which is publicly accessible. Or guess the location from other loaded images by parsing the URL path.
Here is the bit of logic I'm currently trying to use after visiting the documentation for Intervention.
public function store(Request $request) {
$this->validate($request, [
'photo' => 'image|required|mimes:jpeg,png,jpg',
// ...
]);
$image = Image::make($request->file('photo'));
$path = public_path() . '/storage/';
$filename = time() . '__' . $request->file('photo')->getClientOriginalName();
// Save the original image
$image->save($path . "photos/" . $filename);
// Save a scaled down version - Width of 1000px
$image->fit(1000);
$image->save($path . "lr/" . $filename); // lr = low res
// Save the thumbnail - 400px
$image->fit(400);
$image->save($path . "thumbnails/" . $filename);
// Fetch the other values, such as title and description from the request
// ...
$photo = new Photo();
$photo->filename = $filename;
// ...
$photo->save();
return back()->with('success', 'Your image has been successfully uploaded!');
}
This bit of logic isn't working for me at the moment since Intervention keeps throwing a NotWritableException exception. I think this has to do with permissions, I might be wrong. But I have executed the php artisan storage:link command and the error still persists.
Is this way right? Can I just store the full res images in the storage directory and then generate a one-time download link for them? And why am I getting the NotWritableException?
Thanks in advance, Tamart
Please or to participate in this conversation.