404 Page Not Found When Viewing Images Laravel - laravel-5

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...

Related

laravel storage embed pdf not found

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>

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

Shared Hosting Laravel 8 Hosting problem: Symlink is not working

I need the deploy my laravel project to the web. With this introduction But the hosting provider is Doesn't allow me to use a symbolic link(symlink), I called customer service, and told me blocked for security reasons.
So, since I do not use symbolic links, there are big problems in uploading images and deploying them.
Is there a way to do deployment my app?
Change your disk configurations in the config/filesystems.php to read and write to public folder via public_path() function.
If you're deploying over a shared host, you probably have another folder like public_html as your public folder which is not included in your source. Try to link to the public_html using dots ../../public_html/
'public' => [
'driver' => 'local',
// 'root' => storage_path('app/public'),
'root' => public_path(),
'url' => env('APP_URL'),
'visibility' => 'public',
],
'photos' => [
'driver' => 'local',
'root' => public_path('photos'),
'url' => env('APP_URL') . '/photos',
'visibility' => 'public',
],
Another way is just create something like symlink using php, example you can create a route like below:
Route::get(
'/storage/{file}',
function ($file) {
$filepath = "../storage/app/public/".$file;
header("Cache-Control: public");
header("Content-Type: ".mime_content_type($filepath));
header('Content-Length: '.filesize($filepath));
readfile($filepath);
die();
}
);
Maybe not best practice, but it will solve your problem. It's will do same as symlink.

Illuminate\Contracts\Filesystem\FileNotFoundException in Laravel 5.6 using Storage facade

This code is returning an strange error:
$file = Storage::get(Storage::disk('notas_xml')->path('') . 't.txt');
As you can see the file does exist.
Get the file directly from the disk
$exists = Storage::disk('notas_xml')->exists('t.txt');
if ($exists) {
$file = Storage::disk('notas_xml')->get('t.txt');
}
And if you didn't setup notas_xml disk in filesystems.php
$file = Storage::get('public/arquivos/notas_xml/t.txt');
And to use your code, you need to setup a disk like so in config/filesystems.php
'notas_xml' => [
'driver' => 'local',
'root' => storage_path('app/public/arquivos/notas_xml'),
'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
],
And get the file simply like this
$file = Storage::disk('notas_xml')->get('t.txt');
You need to get the file as below code:
Storage::disk('notas_xml')->has('t.txt');
Above has method may be used to determine if a given file exists on the disk:
Please read documentation https://laravel.com/docs/5.1/filesystem#retrieving-files
To better understand all this... The trick is in: config/filesystems.php
If you have this code (which is the default value of Laravel in Github)
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
This Facade Storage will act in the folders
root_laravel_project/storage/app
So if you want to check if an "israel.txt" file exists
if( Storage::exists('israel.txt') ){
echo "File found. And it exists on the path: root_laravel_project/storage/app/israel.txt";
}
Remember that up to this point it has nothing to do with the symbolic link php artisan storage: link
This symbolic link is only to make a folder called "public" within the "storage" folder be part of the public access through HTTP
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Then at the time of doing the sym. You can access the files by http (which are public for any user)
This example assumes that you are using a virtual host (and if not, you must do it as a recommendation for work better locally)
http:// root_laravel_project.test/storage/israel-alvarez.txt
Or so that it is better understood as in the old school without a virtual host
http:// localhost/public/storage/israel-alvarez.txt
Then these urls will look inside your folder
root_laravel_project/storage/app/public/israel-alvarez.txt
Laravel's documentation is somewhat brief and can be confusing regarding this issue. But you just have to remember that one thing is to access through the "storage Facade" (which is the correct way to upload and verify if there are files) and another thing is to access through the http (through url) which is the symbolic link (which is the treatment you already give to users to download files or see if it is a PDF for example).
I hope it helps. Good day

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

Resources