Laravel symbolic link to storage - laravel

I have some problems creating a symbolic link with laravel to access some images that are in my storage ...
I store my images in the folder
storage\app\public\clients
where I have images 1.png, 2.png and so on ...
I used the php artisan command to create the link
php artisan storage: link
The [public/storage] directory has been linked.
now when i try to access in my view the following path does not work
<img id = "thumbnail" src = "{{asset ('clients / 1.png')}}" width = "150px"> </ img>
do I have to do anything else to gain access to the storage folder in public?

This command will create a link between public/storage and storage/app/public.
So, change this:
{{ asset('clients/1.png') }}
To:
{{ asset('storage/clients/1.png') }}
https://laravel.com/docs/5.5/filesystem#the-public-disk

Related

How to fetch images from storage folder from an add-on-domain in laravel?

I have an add-on-domain as public_html/xyz.in. I am uploading images and images are getting stored inside storage/app/public/images folder. However when I am trying to fetch images from that folder, its showing 404 not found. Please help.
<img src="{{ asset('storage/app/public/images/tulips.jpg') }}">
The folder storage is not available to visitors. That's why you need to create a symbolic link from the public directory.
You can use artisan, to get a symbolic link
php artisan storage:link
You need to create a symbolic link of your storage folder. i.e storage/app/public to public/storage. if you have SSH access you can use php artisan storage:link as #wschopohl suggested. Else you can create a php script to create that symlink.
<?php
$target = '/home/user/domain/storage/app/public';
$shortcut = '/home/user/domain/storage';
symlink($target, $shortcut);
?>
After running you can see storage folder in your public folder. Now you can access image with <img src="{{ asset('storage/folder_name/image_name.jpg') }}">
You can create a storage link. run bellow command :
php artisan storage:link
And display:
<img src="{{ asset('public/storage/templates/image.png') }}" class="img img-thumbnail">
Read a full post: How to display the storage folder image in Laravel

How to access image from /storage -laravel- from SPA with vuejs

if i put an image in "storage/app/public/img/logo.png" and execute: 
$ php artisan storage:link
How i get that logo.png in vue component?
<img src="storage/app/public/img/logo.png">
The requested it's ok (200) but that image not render, because is an SPA.
Thank you,
Well, after to do:
$ php artisan storage:link
In my component i used:
<img :src="'../storage/img/logo.png'">
thanks,
When you use the storage:link command, you create a symbolic link between your storage/app/public folder and your public/storage folder.
So now you can find your files at the url $BASE_URL/storage/img/logo.png.
I recommend you to use the asset helper function inside your blade template:
<img src="{{ asset('storage/img/logo.png' }}">
NOTE: be sure to set the proper file permissions on the folder or you get an Unauthorized error when trying to access it.
Check config/filesystems.php (ie: unmodified default):
// If server doesn't support "making symbolic links":
// https://medium.com/#shafiya.ariff23/how-to-store-uploaded-images-in-public-folder-in-laravel-5-8-and-display-them-on-shared-hosting-e31c7f37a737
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
Save an image:
$image = $request->file('image'); // something like that
$slug = 'some-slug-99';
$image->storeAs(
'examples' .'/'. $slug,
$image->getClientOriginalName(),
'public'
);
Create the symlink from /storage/app/public to public/storage (ie: your public html folder which makes asset() work in Laravel Blade templates, I think but don't quote me on that)
$ php artisan storage:link
Place the HTML in your Vue component:
<img
v-for="image in example.images"
:key="image.filename"
:src="`/storage/examples/${example.slug}/${image.filename}`"
>
Also, read the docs: https://laravel.com/docs/8.x/filesystem
When using php artisan storage:link you are creating a simbolic link from public/storage to storage/app/public
I'm used to use asset instead regular path: Try using src="{{ asset('storage/app/public/img/logo.png') }}
It is supposed that if you are using a SPA, when a component renders, it will make a new request to get the img
Hope my answer will help someone,
Create a symbolic link by using this command
php artisan storage:link
now use it in you vue template as following
/storage/path/to/file
routes/web.php
For SPA application, Laravel usually render index blade for all incoming requests.
Route::get('/{any}', function () {
return view('index');
})->where('any', '.*');
I changed this route so that all routes except "/storage/**/." locate index blade page.
This way, you can both render SPA and get uploaded image.
Route::any('{all}', function () {
return view('index');
})->where('all', '^(?!storage).*$');
I assume you have already created symlink using php artisan storage:link command.
Take a look Vue js laravel 8 routing
I don't know why no one write their solution clear.
In Laravel and vuejs
When you store files inside laravel storage do as bellow;
create a symlink by running php artisan storage:link it will create a directory inside your-project/public by the name of storage which is not actually a directory that is a link to project/storage directory, if you navigate to your-project/public you will see all the files uploaded into your storage showing here, now you cannot access your file directly from here.
If you have an image stored in this location: storage/app/public/files/logo.png the path to show the image in vue a component will be /storage/files/logo.png please escape app/public parts.
<b-avatar href="#" :src="'/storage/files/logo.png'"></b-avatar>
I hop it help someone.

Laravel 5.8 how to display image from storage? My Git added

https://github.com/Loctarogar/Admipanel-to-manage-companies/tree/master/storage/app My git.
I linked storage to public directory with
php artisan storage:link,
but i can't show images from there. If i use
<img src="{{ asset ('storage/app/avatars/LOW6Fc2TBH8UoexcXLntQkzncXSDN6OsIt7KLbiG.jpeg') }}">
image doesn't shows. What i am do wrong?
That creates a symlink from public/storage to storage/app/public for you and that's all there is to it. Now any file in /storage/app/public can be accessed via a link like:
http://somedomain.com/storage/image.jpg
OR
One option would be to create a symbolic link between a subfolder in your storage directory and public directory.
For example
ln -s /path/to/laravel/storage/avatars /path/to/laravel/public/avatars
You forgot to enter in the public folder that storage:link creates...
Change
<img src="{{ asset ('storage/app/avatars/LOW6Fc2TBH8UoexcXLntQkzncXSDN6OsIt7KLbiG.jpeg') }}">
to:
<img src="{{ asset ('storage/app/public/app/avatars/LOW6Fc2TBH8UoexcXLntQkzncXSDN6OsIt7KLbiG.jpeg')}}">

Laravel - link to a file saved in storage folder

When we do something like this:
Storage::disk('local')->put('file.txt', 'Contents');
How do you make a link to that file in a view? Something like this:
Download File
there are so many things in documentation and yet not even one example how to create a link to that file
Try this command on your terminal : php artisan storage:link, then laravel storage become public access.
Download File
UPDATE:
According to laravel docs, You can get the URL to the file like this:
use Illuminate\Support\Facades\Storage;
$url = Storage::url('file1.jpg');
Remember, if you are using the local driver, all files that should be
publicly accessible should be placed in the storage/app/public
directory. Furthermore, you should create a symbolic link at
public/storage which points to the storage/app/public directory.
Hope this helps!
Scenario--(working from a fresh Laravel 5.6 project install for reference):
With the existing default paths being /public and /storage/app/public and you want to physically store your logo.png image in the /storage folder and render it on your Welcome page, the process would go something like this:
In your terminal, navigate to your Laravel project folder.
Run command php artisan storage:link
Now create the folder /images in your /storage/app/public folder which gives you /storage/app/public/images.
Look in your /public folder and you will see the (shortcut) subfolders /storage/images
Copy a test image named logo.png into the /storage/app/public/images folder.
In your project's welcome.blade.php under /resources/views paste the
following code over the existing code:
<div class="content">
<div class="title m-b-md">
Laravel
<div>
<a href="/">
<img src="{{url('/storage/images/logo.png')}}" alt="Logo Image"/>
</a>
</div>
</div>
</div>
Save and open your project in your browser and you should see the logo image.
Having said all of that, later on you need a pdf folder to store uploaded pdf
files. Easy, just create a new folder in /storage/app/public called /pdf and
automatically the shortcut will appear in the /public folder. It is a once and "for all" solution.

Image not displaying in view. Laravel

In my Laravel project I'm trying to load an image into my view using blade but it displays as a broken link. I inspect it with firebug and the src is pointing to the image but nothing is displaying.
My image is located in my project's public/images/users/3/profilePictures/
Here is my <img> in the view.blade.php
<img class="com-profile-picture" src="images/users/{{ $follower->id }}/profilePictures/50thumb-{{ $follower->profilePicture->url }}" alt="{{ $follower->first_name }} {{ $follower->last_name }} Profile" width="50" height="50">
However I get this when I load the page:
When I inspect the image with firebug I see this:
That is the correct src and the image does exist in my public/images/users/3/profilePictures/ directory
Anyone know why this is happening?
This may be caused you are in a route which does not represent the base URL. You should generate the URL for your assets relative to the public/ folder. Use URL::asset('path/to/asset') to generate the URL.
{{ URL::asset("images/users/{$follower->id}/profilePictures/50thumb-{$follower->profilePicture->url}") }}
Or as #lukasgeiter mentioned you can simply use asset('path/to/asset') helper.
You can try with the
php artisan storage:link
else you can use this
A simple fix for the issue (linked broken error)
in .env file
APP_URL=http://localhost
this replace with the
APP_URL=http://localhost:8000
then the issue will be fixed.
delete already generated symlink and then run this command
php artisan storage:link
if you are using laravel, consider excluding public folder or any folder where your public files are store in .htacccess from folders that laravel control.
Do it like this in .htaccess
# Exclude directory from rewriting RewriteRule ^(public/) - [L]
Storage location before (Not Working) :
<img src="{{asset('storage/app/public/media/productImages/'.$list->image)}}" alt="" srcset="">
Working storage location :
<img src="{{asset('storage/media/productImages/'.$list->image)}}" alt="" srcset="">
Else try :
Deleting all the linked/shortcut folders created in public folder
Then recreating the link by running :
php artisan storage:link

Resources