Laravel-admin 上传图片不显示

6 篇文章 0 订阅

找了半天解决方案,网上基本都是互相抄,改来改去也没效果,因此自己总结一下。
PHP版本:PHP/7.3.4
Laravel版本:V6+
Laravel-admin版本:1.8.17

  1. 修改config/admin.php
/*
    |--------------------------------------------------------------------------
    | Laravel-admin upload setting
    |--------------------------------------------------------------------------
    |
    | File system configuration for form upload files and images, including
    | disk and upload path.
    |
    */
    'upload' => [

        // Disk in `config/filesystem.php`.
        'disk' => 'public',

        // Image and file upload path under the disk above.
        'directory' => [
            'image' => 'images',
            'file'  => 'files',
        ],
    ],

2.修改config/filesystems.php

/*
    |--------------------------------------------------------------------------
    | 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',
        ],

        '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'),
        ],
         'admin' => [
            'driver'     => 'local',
            // 'root'       => public_path('upload'),
            'root'       => public_path('app/public'),
            'visibility' => 'public',
            'url' => env('APP_URL').'/upload',
        ],

    ],

3.在需要上传图片的控制器中调用上传图片的方法

/**
     * 上传图片专用
     * Author:williamslife@icloud.com
     */
    public function upload_img(Request $request, $fileInputName="image",$path="")
    {
        if ($request->isMethod('POST') || $request->isMethod('PUT')) { //判断文件是否是 POST的方式上传
            $tmp = $request->file($fileInputName);
            // $path = '/article'; //public下的article
            $FileType = $tmp->getClientOriginalExtension(); //获取文件后缀
            $FilePath = $tmp->getRealPath(); //获取文件临时存放位置
            $FileName = date('Y-m-d'). "/" . uniqid() . '.' . $FileType; //定义文件名
            Storage::disk('public')->put($FileName, file_get_contents($FilePath)); //存储文件
            return $path . '/' . $FileName; //文件路径
        }
    }

4.删除storage在public下的软连接(如果有,执行此操作)
在这里插入图片描述
5.清除配置缓存

$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!

6.重新配置链接

$ php artisan storage:link
The [public/storage] directory has been linked.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值