How to upload an image and display as logo

Published 1 week ago by bobmyles

I am using Laravel 5.4. I am creating organizations; whose details include a logo.

The intention is when a user for the organization logs in, the logo is on the display.

I am able to upload and save the logo on storage/app/logos/organization_name/logo... eg storage/logos/Abc/logo.jpg

However, I am not able to pick and display the image/logo for an organization. I have gone through the docs https://laravel.com/docs/5.3/requests#retrieving-uploaded-files but still struggling.

Kindly someone save my day.

Image Upload and save code below:

  $org_image = $request->org_image;

        if ($org_image !== null) {
            $fileext = $org_image->getClientOriginalExtension(); //Get file extension
            $org_image->storeAs('logos/' . $organization_name, "logo.{$fileext}"); //save file 
     
         }

How do I fetch and display this on the view diffentiating by the organization name?

Regards.

Best Answer (As Selected By bobmyles)
bobmyles

This is how I was able to achieve this, well just in case some one else is struggling.

I changed my default disk on the file system to public i.e. (`app/config/filesystem.php)

Then created a symbolic link app/storage/app/public to app/public/storage

This enabled me to access all files on app/public/storage ....

To access the logos on the view I used img src="APP_URL/public/storage/logos/logo.png"

Thank you guys for the assistance .... About this and more here => https://laravel.com/docs/5.4/filesystem#the-public-disk

edoc
edoc
1 week ago (41,020 XP)

Can you try if this works in your view file? storage_path('app/logos/Abc/logo.jpg')

if not

Storage::local('app/logos/Abc/logo.jpg');

bobmyles

@edoc You mean this? <img src="storage_path('app/logos/Demo/logo.jpg')"

edoc
edoc
1 week ago (41,020 XP)

<img src="storage_path('app/logos/Demo/logo.jpg')>

the path should be your file in the storage folder

bobmyles

I tried this

<span class="logo-lg"><img src="Storage::local('app/logos/Demo/logo.jpg');" class="img-responsive" ></span>

this as well;

<span class="logo-lg"><img src="storage_path('app/logos/Abc/logo.jpg')" class="img-responsive" ></span>

but there was no display

edoc
edoc
1 week ago (41,020 XP)

Ok. the path should be your file in the storage folder which I don't know.

Robert-Jan

The uploaded file is outside of the web root (if properly configured) So you have to create a symlink in your public folder to the storage\app\public folder.

P.S. use the public driver when uploading a file instead of local. And you also have to use the moustaches in the src tag like so src="{{ storage_path(...) }}"

bobmyles

Hi Robert, I changed the Default Filesystem Disk to use public ....

This created storage/app/public/logos/bobChild storage path..

The images are stored on the path storage/app/public/logos/bobChild/logo.png How do I create the symboilc link?

Kindly advice.

Robert-Jan

By running this handy command: php artisan storage:link

( https://laravel.com/docs/5.4/filesystem#configuration )

bobmyles

Hi... I tried php artisan storage:linkbut got;

I went though the doc as well.

[ErrorException] symlink(): No such file or directory

I am on ubuntu os ..

Kindly advice

saurabhd

Use below code for display logo image

<span class="logo-lg"><img src="{{storage_path('app/public/logos/bobChild/logo.png')}}" class="img-responsive" ></span>

Hope this work for you !

bobmyles

I have tried this but failed;

<span class="logo-lg"><img src="{{storage_path('app/public/logos/bobChild/logo.png')}}" class="img-responsive" ></span>
edoc
edoc
1 week ago (41,020 XP)

paste the link to the image

bobmyles

Below is the abs path to the image;

<span class="logo-lg"><img src="/var/www/html/airtime_top_up_portal/storage/app/public/logos/bobChild/logo.png" class="img-responsive" ></span>
    

Is this what you need?

Snapey
Snapey
1 week ago (454,775 XP)

Hi Bob

If your website is correctly configured then there is no way you should be able to access any files that are not in the public area.

so this;

<span class="logo-lg"><img src="/var/www/html/airtime_top_up_portal/storage/app/public/logos/bobChild/logo.png" class="img-responsive" ></span>

should never work because this path is not externally accessible.

its important to appreciate the difference between the path (the place on the server where the image is located) and the URI (the external method of requesting that file)

<img src=""> Can only use the URI

You have two options.

1 move the image into your public folder (or a subfolder), for instance /public/logos/bobChild.png.

/logos/bobChild.png would then be the URI needed to access this image. It is the URI that you need in the image tag src= in order for a browser to load the image.

2 store the image in your storage/app/public folder and then create a symlink so that you have an internal path to the image which is different from the external URI You can read all about this technique here https://laravel.com/docs/5.4/filesystem#the-public-disk

if you run the command php artisan storage:link and you get an error, its either because the folder storage/app/public does not exist or there is already a real folder called public/storage

bobmyles

This is how I was able to achieve this, well just in case some one else is struggling.

I changed my default disk on the file system to public i.e. (`app/config/filesystem.php)

Then created a symbolic link app/storage/app/public to app/public/storage

This enabled me to access all files on app/public/storage ....

To access the logos on the view I used img src="APP_URL/public/storage/logos/logo.png"

Thank you guys for the assistance .... About this and more here => https://laravel.com/docs/5.4/filesystem#the-public-disk

Sign In or create a forum account to participate in this discussion.