laravel storage embed pdf not found - laravel

I want to embed a PDF file into a modal blade view. The disk is not public (because it shouldn't be) and with a logged in user, I get the 404 not found message.
Inside config/filesystems.php I got:
<?php
'disks' => [
'custom' => [
'driver' => 'local',
'root' => storage_path('app/custom'),
],
Inside my blade view I already tested successfully
<iframe src="https://www.inkwelleditorial.com/pdfSample.pdf" width="100%" height="300"></iframe>
However, when I try:
<iframe src="{{ storage_path('app/custom/myFile.pdf')}}" width="100%" height="300"></iframe>
I get
404 NOT FOUND
I have made sure the file does exist:
dd(Storage::disk('custom')->path('myFile.pdf')) => shows absolute path correctly
dd(Storage::disk('custom')->exists('myFile.pdf')) => returns true.
How do I fix this?

First,you must know that storage_path() is a private path.
if you want show storage file on web, you can see the config file config/filesystems.php
'links' => [
public_path('storage') => storage_path('app/public'),
],
run this command: php artisan storage:link , then go to public directory,you can see the soft link directory on the public directory.
Then you can use the url on webpage like this <ifram src="/storage/app/custom/xxx.pdf" ></iframe>

Related

Laravel 8 - Get image url from images saved in storage folder

I am using Laravel Framework 8.62.0 and save uploaded images like the following:
$contents = file_get_contents($imgUrl);
// $file_name = basename($url_upper);
$file_name = strtolower(substr($imgUrl, strrpos($imgUrl, '/') + 1));
$strg = Storage::disk('public_collectible_img')->put($file_name, $contents);
My public_collectible_img-driver looks like the following:
'public_collectible_img' => [
'driver' => 'local',
'root' => storage_path('app/public/collectibles_img'),
'url' => env('APP_URL').'/collectible/image',
'visibility' => 'public',
],
When I try to open the following url localy I get a 404-error.
http://localhost/my_project/public/collectible/image/1.jpg
The image exists in the folder:
How to access the image and what is the correct url?
I appreciate your replies!
You have to create the a symbolic link between "storage" and "public" folder
To make these files accessible from the web, you should create a
symbolic link from public/storage to storage/app/public.
Run the following artisan code:
php artisan storage:link
Reference: https://laravel.com/docs/8.x/filesystem#the-public-disk

LARAVEL 7 - Image not showing after config:clear

I'm working on Laravel 7 (using Laragon 4.0 on Windows 10), I have some file download and save from remote urls. Everything was working fine but I had to perform a "php artisan config:clear" to update mysql configuration and after that the images saved in storage/public folder are not served anymore.
The files were there but just not served, then I deleted everything inside /storage/public folder, deleted the symlink inside /public folder to /storage (naturally performing "php artisan storage:link") and populate again /storage/public with folders and file like I did previously. The folder structure is there, the files also but when I try to access them I get 404 error.
I tried "php artisan optimize:clear" and every suggestion I found on stackoverflow but nothing seems to help me, I don't know where to look, please help me.
Here is the code I use to download/save images:
$url = $team["logo"];
$contents = file_get_contents($url);
$name = substr($url, strrpos($url, '/') + 1);
$file = 'public/team/logo/'. $name;
Storage::put('team/logo/'. $name, $contents);
This is how I display images on the view:
<img src="{{Storage::url($team->logo)}}" style="max-width:50px">
This is my public disk configuration on filesystems.php
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
As your driver is set to
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Your Storage::disk('local') is, in absolute terms, set to /storage/app/public. Hence $file = 'public/team/logo/'. $name; becomes $file = 'team/logo/'. $name;
In case you are using the Intervention/Image library, you may consider to change
$contents = file_get_contents($url);
to
$contents = File::make($url);
see http://image.intervention.io/api/make

How to create Laravel Storage link to root after remove public folder

I removed public folder Now I want to create storage link to root folder name upload
filesytems.php
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/upload',
'visibility' => 'public',
],
When I use php artisan Storage:link it's create a public folder. But I don't want this.
I also try
ln -s storage/app/public upload
But got error massage
'ln' is not recognized as an internal or external command,

Restrict direct access to image url in laravel

If i give image path directly it accessible now. I want image accessible only for if user loged in.
Ex:
This is image path http://localhost:8000/images/test.jpg
If user give this path without loged in through error message as access dined
Can you help me..
There are several ways to fix this problem, the way I would use is to save all images in your resources/assets/ folder outside of the public folder.
Now you could create a route like this:
Route::get('images/{file}', 'ImageController#getImage')->where('file', '.*');
And now in your ImageController, you can check if the user is logged in. If so, you can load the image from the resources folder and send it in a file response. If they are not logged in, there is no way they will be able to access the file.
Tested with Laravel 8
Define a new disk in file: config/filesystem.php
'disks' => [
// ...
'private' => [
'driver' => 'local',
'root' => storage_path('app/private'),
'url' => env('APP_URL').'/storage',
'visibility' => 'private',
],
],
Define web route in file: routes/web.php
Route::any('/private{any}', function (Request $request, $file) {
abort_if(!Storage::disk('private')->exists($file), 404, "File doesn't exist.");
return Storage::disk('private')->response($file);
})->middleware('auth')->where('any', '.*');
Upload image file to folder: storage/private
Test on your localhost at: http://localhost:8000/private/image_name.jpg

404 Page Not Found When Viewing Images Laravel

I have an image stored in the database as conventions/convention_title.jpg
The file is located in "storage/app/conventions/convention_title.jpg"
When i do something like this flyer }}">, it doesn't display...
OR
flyer }}"> the image still wont display...
And even worse still, if i try to access the image directly via link like this...
http://localhost:8000/storage/app/convention/convention_title.jpg i get a view not found error -> 404 page not found..
What could be the cause of this?
php artisan storage:link
problem solved while writing
src=" {{ asset('/storage/'.$post->image) }}"
Web content must be stored on the laravel root /public folder and you can easily access to it via asset helper.
For example:
/public/img/conventions/convention_title.jpg
Then you can access on the views to it like:
{{ asset("/img/conventions/convention_title.jpg")}}
In case you want to test if you can access to the image you can try:
http://localhost:8000/convention/convention_title.jpg
You can create a new disk in the config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]
Then use it :
Storage::disk('uploads')->put('filename', $file_content);
Or you can do this simple method
$request->file('photo')->move(public_path("/uploads"), $newfilename);
Then, using any of the above methods, you can still access your images using the asset() helper function...

Resources