0

The photos in Laravel 8 are not showing in html "the alternative text is showing instead". In windows file explorer, I found each photo as a folder with the name of the photo for example "myphoto.jpg" and inside this folder there is a ".tmp" file. the destination folder is "uploads/posts" and when I inspect the photo in the browser, the link is shown correctly. here's a link example of one photo: "http://localhost/laravel/blog/uploads/posts/1657160399photo.jpg".

and when I access it directly from the browser, I get "404 Not Found". I tried to add the following code inside filesystems.php inside "links" but it didn't work as well, the code line is:

public_path('uploads/posts') => storage_path('app/uploads/posts')

and in the terminal I also executed the following command but to no avail:

php artisan storage:link

I'll put below some related code from filesystems.php:

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

and

 'links' => [
        public_path('storage') => storage_path('app/public'),

        //added by me
        public_path('uploads/posts') => storage_path('app/uploads/posts')
    
    ],

and one last note .. I used .htaccess in my root folder just to get rid of "public" from the link.. for example if I want to go to posts page I write :

http://localhost/laravel/blog/posts

insted of: http://localhost/laravel/blog/public/posts

Thanks in advance.

php_man
  • 55
  • 7
  • welcome to stackoverflow php_man. did you verify that the symlinks are made? what os are you using? have you tried to narrow down the problem by **disabling** that rewrite rule that removes `public` from your url? – Bagus Tesa Jul 08 '22 at 20:44
  • Thank you for the comment @BagusTesa .. I tried to remove the .htaccess, same problem .. the symlinks that I mentioned above "inside filesystems.php" -> I'm not sure if this is a correct way' .. The OS is windows .. – php_man Jul 08 '22 at 20:45
  • `filesystem.php` is not the `symlink`. its a config telling laravel which stuff are where. [you can check whether you have `symlink` using `dir` command](https://stackoverflow.com/questions/10188485/windows-symbolic-link-target). – Bagus Tesa Jul 08 '22 at 21:06
  • Thanks again @BagusTesa , The thing is, I changed the function used to save the images from move( ) to store( ), then I found the images getting saved inside: 'Storage/uploads/posts' insted of 'uploads/posts' folder that I created.. so what do I need to do next if you could help please .. – php_man Jul 08 '22 at 21:59
  • @BagusTesa even if I open the file in the browser with the link in storage/uploads/posts , it will give me 404 Not Found .. the same exact link .. and the image is there with the original extenstion (not tmp) .. still 404 :'( – php_man Jul 08 '22 at 22:26

1 Answers1

0

You will have to create a file response in Blade as the img source

<img src="{{ response()->file($pathToFile) }}">

For more information check https://laravel.com/docs/9.x/responses#file-responses

Dimitri Mostrey
  • 2,302
  • 1
  • 12
  • 11
  • Thanks Dimitri, I tried it but unfortunately the image still doesn't show and if I inspect it and open its link from the browser, it gives me 404 not found .. while the image exists in file explorer .... – php_man Jul 09 '22 at 03:43
  • well, should we add the photos in the public folder? cuz it worked when I tried in the public folder ! .. is that mandatory ?.. also what should we do to prevent direct access to these photos? becuase they should be private as they are used as profile pictures – php_man Jul 09 '22 at 17:33
  • Sorry for the late answer. Yes, the picture has to be publicly available. Either under /public but better under /storage/app/public. Don't forget to create the symlink. These 'disks' are already predefined in the /config/filesystem.php settings file. – Dimitri Mostrey Jul 10 '22 at 13:23