Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tanmay_das's avatar

Displaying stored images on shared hosting

I have successfully deployed my first laravel application on a live server. Everything looks great except the fact that I am unable to display the images that are being uploaded to the storage/app/public/myfolder1 folder.

Here is my folder hierarchy on HostGator:

/myproject_src/ : Here are all the laravel source files (except the public folder)

/public_html/mydomain.com/ : Here goes all my contents of the public directory

I am storing the file path into the database in the following manner:

public/myfolder1/FxEj1V1neYrc7CVUYjlcYZCUf4YnC84Z3cwaMjVX.png [This path is associated with the image that has been uploaded to storage/app/public/myfolder1/ this folder]

What should I do in order to display the images properly in a img tag:

<img src="{{ how to point to the uploaded image here }}">

0 likes
15 replies
Snapey's avatar

you need to be able to create a symlink from storage/app/public to your external, public area

tanmay_das's avatar

On local environment I would run php artisan storage:link to create a symlink, but I cannot run it on server due to the hosting plan limitation. Any other way to create the symlink? I have the SSH access though.

Snapey's avatar

...or store your files in the public folder?

tanmay_das's avatar

But people keep telling about all this 'Conventions' and 'Best Practices' where they suggest not to put them in public folder?

Snapey's avatar

it's no different since the symlink makes the folder externally accessible anyway

1 like
tanmay_das's avatar

@Snapey How do I upload them in the public directory? Currently when I call the store method, I call it this way: ->store('public/myfolder1'); and that uploads the file to storage/app/public/myfolder1 in this folder.

My document root is: /public_html/mydomain.com/ what would be the passed argument for the store() method, if I wanted to store an image, say on /public_html/mydomain.com/imgs/myfolder1 ?

Snapey's avatar

you can create a new disk in the config (or change the existing disk).Sorry, I'm on iPad at the moment so don't know exactly but I think it is in config/storage.php

jlrdw's avatar
<?php if (empty($row->dogpic)) { ?>
                        <td class="imgtd"> </td>
                    <?php } else { ?>
                        <td class="imgtd"><a href="<?php echo DIR . 'dog/view?dogid=' . $row->dogid; ?>"><img src="<?php echo DIR . 'upload/imgdogs/' . $row->dogpic; ?>" alt="" class="image"></a></td>
                    <?php } ?>

Or template language

<div id="tbl-container">
    <table class="cattable">
        <tr>
            <th class="imgtd">Catpic</th>
            <th class="cname">Name</th>
            <th class="csex">Sex</th>
            <th class="catcomment">Comments</th>
        </tr>

        @foreach ($cats as $row)
        <tr>
            @if (empty($row->catpic))
            <td class="imgtd"> </td>
            @else
            <td class="imgtd"><a href="{{ DIR . 'cat/view?catid=' . $row->catid}}" ><img src="{{ DIR . 'upload/imgcats/' . $row->catpic }}" alt="" class="image"></a></td>
            @endif
            <td class="cname"> {{ $row->catname }} </td>
            <td class="csex">{{ $row->sex }}</td>
            <td class="catcomment">{{ $row->comments }}</td>
        </tr>
        @endforeach
    </table>

</div>

<table style="width:80%;">
    <tr>
        <td> {{ $pageLinks }} </td>
    </tr></table>
<br>


DIR is path I resolved. A constant I made.

Edit you have to resolve paths, i.e., when you see things like

/../../../whatever/myimages

that is path resolving

Forget about laravel, path resolving is necessary no mater weather

php, java, asp, asp.net , coldfusion, a local windows machine where you are going to copy a file from one folder to another, etc etc.

wari's avatar

@tanmay_das have you fixed this issue? please if you fixed let me know how have you fixed? I am having the same issue.

Thanks

patricpoba's avatar

There is another way to create a symlink without ssh access. Use this code

  1. Create a file in your public_html file with a name (eg symlink-gen.php) with the code below

< ?php

$linkName = '/home/username/public_html/mydomain.com/public/myfolder1';

$target = '/home/username/public_html/mydomain.com/storage/app/public/myfolder1';

symlink($target, $linkName);

?>

Please modify it to suit your needs.

  1. The run the file from your browser like this. www.mydomain.com/symlink-gen.php. If no errors are returned, then it was successful.

Reference: http://php.net/manual/en/function.symlink.php

1 like
tanmay_das's avatar
tanmay_das
OP
Best Answer
Level 4

@helpmyworld Yes I had. If my memory serves me correctly, this is what I had to do in order to display the images:

  1. In my filesystems.php file, I had changed the storage path of the local driver from 'root' => storage_path('app') to: 'root' => '/home3/tanmay/public_html/mydomain.com/storage/app'.

  2. Then I referred to the images like this: <img src="{{ asset('storage/app/public' . str_replace('public','',$image->imgsrc)) }}" alt="{{ $ad->name }}" class="img-responsive img-thumbnail" height="200" width="200">

  3. storage/app/public/myfolder1 directory is where I used to store the images

2 likes
rafaellops's avatar

I have a site hosted on a shared HostGator server, I was able to solve the problem as follows:

For security reasons, I left the /public_html directory only the files in the public directory of Laravel, the rest of the files were in a directory on the same level as public_html. Example: /home /username/projectname

I did not change any settings in filesystem.php, I just created a symbolic link to the storage directory.

To create the symbolic link, I accessed the server with the PuTTY software (SSH), entered the public_html directory and executed the command ** **ln -s /home/username/projectname/storage/app/public /storage

The "php artisan storage: link" command did not work for me because it creates the symbolic link inside /home/username/projectname/public / storage.

And to show the image: '<img src = "{{asset ("storage/$image->imgsrc")}}" />'

1 like

Please or to participate in this conversation.