Look at my code. This code store and read file from non-public dir but if I copied img url.
$filename = date('YmdHi') . self::setUser()->id . rand(1, 99999999);
$img = Image::make($file)
->encode($extension, 100)
->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
})
->stream();
$storage_path = storage_path('app/uploads/' . $path);
if(!Storage::exists($storage_path)){
Storage::makeDirectory($storage_path, 0755, true, true);
}
file_put_contents($storage_path . '/' . $filename . '.' . $extension, $img);
$filename = $filename . '.' . $extension;
//read the file
$path = 'uploads/photo';
$image_path = storage_path('app/' . $path . '/' . $filename);
if (file_exists($image_path)) {
$file = Storage::get($path . '/' . $filename);
$type = mime_content_type($image_path);
$base64 = base64_encode($file);
$img = "<img src='data:{$type};base64,{$base64}'>";
echo $img;
exit;
}
I need to protect my file becouse user need to permission to display particular photo. Currently any user can copy this url and any guest without permission could display that. How can I protect my photos? I tested that in local (sail).