图像未显示在laravel项目中

acruukt9  于 2023-01-31  发布在  其他
关注(0)|答案(6)|浏览(159)

现在,我在Laravel项目工作,但我在这个项目中得到了一些问题。我存储在数据库中的图像名称,也存储在存储文件夹中,但图像不显示在我的 Jmeter 板

刀片文件代码:

<div class="col-sm-8">
  <a href="blog_detail/{{ $main_blog->id }}">
    <img src="<?php echo asset("../storage/app/assets/upload/images/$main_blog->image")?>" class="blog_img">
    <h1 class="blog_main_heading">{{ $main_blog->title }}</h1>
  </a>
</div>
cczfrluj

cczfrluj1#

我看不到你的文件夹结构,希望它的作品虽然,请检查下面的代码:

<div class="col-sm-8">
  <a href="blog_detail/{{ $main_blog->id }}">
    <img src="{{ Storage::url('app/assets/upload/images/'. $main_blog->image) }}" class="blog_img">
    <h1 class="blog_main_heading">{{ $main_blog->title }}</h1>
  </a>
</div>
wvyml7n5

wvyml7n52#

如果您映像storage path是这样的,那么它将适合您。public/assets/images/default.png

<img src="{{asset('assets/images/default.png')}}" alt="">
j91ykkif

j91ykkif3#

{{ url(asset('images').'/'.$img_name) }}

而不是图像使用您的路径并尝试。请检查图像是否存在于您的服务器中

d5vmydt9

d5vmydt94#

首先,值得一提的是,您的代码是一个糟糕的实践,最好在控制器中执行类似的操作,比如ImageController

class ImageController extends Controller
{
    private $storagePath;
    public function __construct()
    {
         $this->storagePath = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix();
    }

    public function index(){
         $images = Image::all();
         $storagePath = $this->storagePath;
         return view('index', compact('images', 'storagePath'));
    }
}

然后在刀片中

<div class="col-sm-8">
        <a href="blog_detail/{{ $main_blog->id }}">
            <img src="{{ $storagePath . $main_blog->image) }}" class="blog_img">
            <h1 class="blog_main_heading">{{ $main_blog->title }}</h1>
      </a>
    </div>

关于这个问题,你应该检查一下丢失的图片,看看网址是什么。检查一下网址中是否有/public后缀。让我们看看filesystem.php。这是我的

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. The "local" disk, as well as a variety of cloud
    | based disks are available to your application. Just store away!
    |
    */

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been setup for each driver as an example of the required options.
    |
    | Supported Drivers: "local", "ftp", "sftp", "s3"
    |
    */

    'disks' => [

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

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL') . '/storage',
            'visibility' => 'public',
        ],
        'temp' => [
            'driver' => 'local',
            'root' => storage_path('app/temp'),
            'url' => env('APP_URL') . '/storage',
            'visibility' => 'public',
        ],
        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Symbolic Links
    |--------------------------------------------------------------------------
    |
    | Here you may configure the symbolic links that will be created when the
    | `storage:link` Artisan command is executed. The array keys should be
    | the locations of the links and the values should be their targets.
    |
    */

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

];

适当地设置此文件并确保存储链接在生产服务器上运行。无论如何,我们需要更多详细信息来调试您的问题。

u3r8eeie

u3r8eeie5#

首先,asset()助手是基于您的APP_URL,这意味着您试图直接访问存储路径,这将不起作用,因为您无法访问公共文件夹。
所以,确保你做了php artisan storage:link,当你这样做的时候,它会创建一个从你的存储到公共的链接,你可以看到你的公共文件夹下的存储文件夹,然后你就可以像这样使用asset('storage/app/assets/upload/images/filename.png')
如果您在公用文件夹中找不到存储文件夹,则表示链接构建失败。

polhcujo

polhcujo6#

在网页中显示图像URI,复制并粘贴图像URI以检查图像URI是否正确
你用的是php artisan storage:link吗?

相关问题