Laravel image not shown on server and working fine on my local machine

岁酱吖の 提交于 2021-01-05 10:48:18

问题


On my local machine project working file but when i uploaded on my server then all images are not shown, and NotFoundHttpException error when i hit full image path to browser.

mypath to project is as

/public_html/offlinemall/public 

And Below is my 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',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],
    'featured' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

],

And how i get image

<img style="height: 480px" src="storage/{{ $ad->avatar }}">

In inspect i get full path but when i hit url then i get NotFoundHttpException error.

http://compare.theofflinemall.com/storage/featured_image/qzHWpQeSfKjZ6DOS59ROyYboJ1GCvVi6NNVfLtVV.jpeg

回答1:


Your server can't locate storage as path because it uses public_path run

php artisan storage:link

It should work. Also, try to use

{{ asset('storage/'.$ad->avatar) }}



回答2:


Basically it has symbolic issue. Probably you have used storage directory to store images. If you use it you need to create a symbolic link by php artisan storage:link. It creates a symbolic directory into your local project's public directory with storage name. When you upload the project on server the symbolic directory won't upload with your project. So, reasonably the browser won't get the path and doesn't show the image. In these perspective there are many solutions:

Solution 1: ssh key access, if you have your server's ssh key access, login into your server using ssh key, go to your project directory and run the command php artisan storage:link it will create the symbolic directory to your project.

Solution 2: By creating cron job on the server.

Solution 3: By executing php script to create symbolic link. Please go through this link cron job and php file execution.

Solution 4: By changing the root directory of image path in config/filesystem.php. please go through this url laracast.com




回答3:


You can change image tag to:

  1. <img src={{ asset('storage/'.$ad->avatar) }} />
  2. <img src="storage/app/public/{{$ad->avatar) }}" />
  3. <img src="<?php echo asset("storage/app/public/$ad->avatar")?>"></img>


来源:https://stackoverflow.com/questions/45680545/laravel-image-not-shown-on-server-and-working-fine-on-my-local-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!