Correct link to storage in laravel - php

I want to display avatar in my page but I do not know how to do it. Look, here's my script. It doesn't work. Can you help me?
#foreach($user->user_data as $data)
<img alt="" src="{{ asset('/storage/uploads/avatars/ {{ $data->avatar }} ') }}">
#endforeach

There is a storage_path helper in laravel. Try using this:
#foreach($user->user_data as $data)
<img alt="" src="{{ storage_path('uploads/avatars/' . $data->avatar) }}">
#endforeach
Link to the doc: https://laravel.com/docs/5.5/helpers#method-storage-path

If you have installed Laravel the normal way you can't link to the storage folder directly because it's located outside of the public folder.
Only the contents of the public folder are accessible to the outside world.
There are two ways to work around this limitation, depending on how sensitive the stored data is.
If anyone may access the file, the easiest solution is to create a symlink from inside your public folder to the avatar storage folder.
If the file needs to be protected, create a Controller class that authorises the user and then returns the desired data from the storage_path.
In your case it sounds like solution 1 would suffice, and fortunately for you Laravel has built-in support for this.
By executing the following command in your command line Laravel will create a symbolic link from public/storage to storage/public:
php artisan storage:link
You can then create a link to anything that is stored in storage/public by using the asset function
asset('storage/path_relative_to_storage_public')
For a more detailed explanation, see: https://laravel.com/docs/5.5/filesystem#the-public-disk

In Laravel, code inside {{ }} will be interpreted as php code, You can Try this One :
#foreach($user->user_data as $data)
<img alt="" src="{{ asset('/storage/uploads/avatars/'. $data->avatar) }}">
#endforeach
It's like you do:
<img alt="" src="<?php echo asset('/storage/uploads/avatars/'. $data->avatar); ?>">

Related

How do I add images on my laravel project?

I am trying to add images so I can see it on my views. I put the folder img inside the public folder and this is the code I am trying to see.
<?php $image = public_path() . './img/logo.png'; // destination path ?>
<h1>Royal <img src="{{$image}}"> Dice</h1>
All I get is this error in the console: Not allowed to load local resource.
I am learning Laravel but I think that this should be easy to solve. Thank you everyone!
Following should work:
<h1>Royal <img src="{{ asset('img/logo.png' }}"> Dice</h1>
You can find more about the asset helper in the laravel documentation: https://laravel.com/docs/8.x/helpers#method-asset
wite like this <h1>Royal <img src="{{ URL::to($image) }}"> Dice</h1>
or
<h1>Royal <img src="{{ asset($image) }}"> Dice</h1>

Laravel 6 Blade - Display all images inside a public folder?

I am having trouble figuring out a way to reach and display the contents of a folder located in 'public/images'. The goal is to have a blade file that acts as a gallery and displays all the images added to that specific folder.
I know how to access specific files using asset('images/imagename.png') but I need to get all of them at once.
Can I simply create a loop inside of the blade file or do I need to make a controller/route for it?
Try this:
#foreach(File::glob(public_path('images').'/*') as $path)
<img src="{{ str_replace(public_path(), '', $path) }}">
#endforeach
You could do use glob method of the File facade if you know the directory. The following code assumes every file in the public/images/ folder is an image.
<div class="container">
#forelse (File::glob('public/images/*') as $file)
<img src="{{ $file }}">
#else
<p id="no-images">No images in folder</p>
#endforelse
</div>
As the documentation implies, you can get all files in a specific directory with:
Storage::files($directory);
So, you can iterate like this:
#foreach(Storage::files($directory) as $file)
<img src="{{ $file }}">
#endforeach

images not accessible from public storage folder

I try to access an image that is in following location:
storage/app/public/uploads/myImageIsHere.jpeg
I have tried all stack overflow options, public_path, Storage::url, creating a symlink link and thousands others.
Can someone explain me how to access this image in my .blade file ??
<img src="{{ Storage::url("app/sponsors/8R4HnbOzDdUilnSaZSRj9VzmeI7oKI9JrCWe3rgY.jpeg") }}">
GET http://127.0.0.1:8000/storage/app/sponsors/8R4HnbOzDdUilnSaZSRj9VzmeI7oKI9JrCWe3rgY.jpeg 404 (Not Found)
And this returns a correct path according to my img src path.
Thanks a lot!
Did you try ,
Storage::get("app/public/uploads/myImageIsHere.jpeg");
https://laravel.com/docs/5.8/filesystem
use this instead
<img src="{{ URL::to("/images/abc.jpg") }}">
this will return an image namely abc.jpg inside "public/images" folder.
Run php artisan storage:link to create a symlink to your public storage disk. After that you can access your image like that:
<img src="{{ 'storage/uploads/8R4HnbOzDdUilnSaZSRj9VzmeI7oKI9JrCWe3rgY.jpeg' }}">

How to fix GET http://localhost/storage/users/default.jpg 404 (Not Found) in laravel

I'm facing a problem that return me :
GET http://localhost/storage/users/default.jpg 404 (Not Found).
I have storage link (a shortcut) created and a users/default.jpg file in it.
I've called it in blade like this :
{{Storage::disk('public')->url('users/'. Auth::user()->image)}}.
But it's not working. Any one can help me? How can i fix this issue. I can't find the problem.
My code :
<div class="user-pic">
<img src="{{Storage::disk('public')->url('users/'. Auth::user()->image)}}" alt="users" class="rounded-circle" width="50" />
</div>
Storage folder can not be called publicly, you should create a symlink using
php artisan storage:link
and then you can use
$path = asset('storage/' . $filepath/$fileName);
to retrieve the file,
refer docs
You can direct access your public folder by writing this <img src="{{ asset('imgs/logo/global.png') }}"/> it will take path of local like /public/imgs/logo/global.png
After running the command php artisan storage:link, you'll end up accessing your assets files in the storage directory using this syntax : asset('storage/file.txt');
If you wish access it inside a blade template, you'll write something like this :
<img src= "{{ asset('storage/users/' . Auth::user()->image) }}">

Img not displaying correctly in Symfony

My twig file goes like this :
Let us try to see an image :
<img src="{{ absolute_url(asset('app/Resources/images/bulb.png')) }}" alt="Symfony!" width="42" height="42"/>
Trying it another way :
<img src="app/Resources/images/bulb.png" alt="Symfony!" width="42" height="42"/>
But when I go to that page in Symfony, I see something like this :
What did I do wrong ?
currently, twig is going to be trying to find your asset in:
/web/app/Resources/images/bulb.png
use instead:
{{ asset('#AppBundle/Resources/public/images/bulb.png', absolute=true) }}
note the use of an additional public folder. If you must store assets in app, then this is sensible.
However, #Ewan Delanoy is correct, you really should be storing all your assets directly in the web folder.
Then you can just call
{{ asset('images/bulb.png', absolute=true) }}

Categories