laravel 七牛文件上传

安装Laravel七牛扩展包

过Composer安装

composer  require zgldh/qiniu-laravel-storage

接下来在 config/filesystems.php 里的disks中新增七牛配置:

'qiniu' => [
    'driver'  => 'qiniu',
    'domains' => [
        'default'   => 'xxxxx', //你的七牛域名
        'https'     => 'xxxxx',         //你的HTTPS域名
        'custom'    => 'xxxxx',     //你的自定义域名
     ],
    'access_key'=> '',  //AccessKey
    'secret_key'=> '',  //SecretKey
    'bucket'    => '',  //Bucket名字
    'notify_url'=> '',  //持久化处理回调地址
],
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use zgldh\QiniuStorage\QiniuStorage;
class UploadController extends Controller
{
    /**
     * 上传文件到七牛
     * @author 高伟
     * @date   2016-11-09T16:58:37+0800
     * @param  Request                  $request [description]
     * @return [type]                            [description]
     */
    public function uploadFile(Request $request)
    {
        // 判断是否有文件上传
        if ($request->hasFile('file')) {
            // 获取文件,file对应的是前端表单上传input的name
            $file = $request->file('file');
            // Laravel5.3中多了一个写法
            // $file = $request->file;
 
            // 初始化
            $disk = QiniuStorage::disk('qiniu');
            // 重命名文件
            $fileName = md5($file->getClientOriginalName().time().rand()).'.'.$file->getClientOriginalExtension();
 
            // 上传到七牛
            $bool = $disk->put('iwanli/image_'.$fileName,file_get_contents($file->getRealPath()));
            // 判断是否上传成功
            if ($bool) {
                $path = $disk->downloadUrl('iwanli/image_'.$fileName);
                return '上传成功,图片url:'.$path;
            }
            return '上传失败';
        }
        return '没有文件';
    }
}

猜你喜欢

转载自blog.csdn.net/qq_30202073/article/details/84566746
今日推荐