七牛上传

浏览:597 发布日期:2018/03/20 分类:技术分享 关键字: 七牛云上传
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
/**
 * [qn_upload 七牛上传]
 * @author 984270382@qq.com
 * @copyright passboat
 * @param     [type]   $file [description]
 * @return    [type]         [description]
 */
function qn_upload($file){
    $qiniu = 'http://*********/';
    $path = date('Ymd/');
    $url = 'public/uploads/'.$path;
    if(!file_exists($url)){
        mkdir ($url,0777,true);
    }
    $name = time().$file['name'];
    $statu = strtolower(substr($name,strrpos($name,'.')+1));
    $allow_type = array('jpg','jpeg','gif','png');
    if(!in_array($statu, $allow_type)){
        return ['code'=>-1,'msg'=>'图片格式错误']; 
    }
    if($file['size']/1024/1024 > 10){
        return ['code'=>-1,'msg'=>'图片大小超过10M!']; 
    }else{
        // 先存储到 本地服务器 
        move_uploaded_file($file['tmp_name'],$url.'/'.$name);
        $accessKey = 'u0P7qA1I83CKtl***********XrwpeLoLm4-oF';
        $secretKey = 'ztRc**************MBZ47wIjwAsbeve2kNH';
        $bucket = 'h****g';
        $auth = new Auth($accessKey, $secretKey);
        $token = $auth->uploadToken($bucket);
        $filePath = $url.'/'.$name;
        $uploadMgr = new UploadManager();
        // 从本地服务器 上传到 七牛
        list($ret, $err) = $uploadMgr->putFile($token, $filePath, $filePath);
        if ($err === null) {
            return ['code'=>-1];
        }
        // 将数据保存到数据库
        // ...........
        return ['code'=>1,'url' => $qiniu.$url.$name];
    }
}
composer require qiniu/php-sdk //composer 自己下载
七牛文档https://developer.qiniu.com/kodo/sdk/1241/php
最近使用到很多接口,本人比较懒,都是用别人的sdk ,希望对你有用,
欢迎关注 欢迎指正!!!
最佳答案
评论( 相关
后面还有条评论,点击查看>>