腾讯云cos图片上传以及上传类

<?php
require(__DIR__.'/../vendor/qcloud/cos-sdk-v5/cos-autoloader.php');
use Qcloud\Cos\Api;
use Guzzle\Service\Resource\Model;
/**
* 腾讯云上传类
*/
class TenUpload extends  Model
{   
    private $cos_client;
    private $CI;
    function __construct()
    {   
        $CI                 = &get_instance();
        $this->CI=$CI;
        $CI->load->config('services');
        $this->cos_client = new Qcloud\Cos\Client($CI->config->item('region'));
    }
    /**
     * 创建空间目录
     * @Author   
     * @DateTime 2018-04-04
     * @version  1.0
     * @param    [type]     $buket [description]
     * @return   [type]            [description]
     */
    public function createBuket($buket){
        try {
            $result = $this->cos_client->createBucket(array('Bucket' => $buket));
            return true;
        } catch (\Exception $e) {
            // echo "$e\n";
            return false;
        }
    }
    /**
     * 删除空间目录
     * @DateTime 2018-04-04
     * @version  1.0
     * @param    [type]     $buket [description]
     * @return   [type]            [description]
     */
    public function deleteBuket($buket){
        try {
            $result = $this->cos_client->deleteBucket(array(
                'Bucket' => $buket));
            return true;
        } catch (\Exception $e) {
            // echo "$e\n";
            return false;
        }
    }

    /**
     * 上传文件类
     * @Author   
     * @DateTime 2018-04-04
     * @version  1.0
     * @param    [type]     $buket [description]
     * @param    [type]     $key   [description]
     * @param    [type]     $body  [description]
     * @return   [type]            [description]
     */
    public function uploadFile($buket,$key,$body){
        set_time_limit(0);
        if(filesize($body) < 5242880){
            //判断文件是否小于5m
              try {
                $result=$this->cos_client->putObject(array(
                            'Bucket' => $buket, 
                            'Key' => $key,
                            'Body' => fopen($body,'rb'),
                            // 'ACL' =>'public-read'
                        ));

                 $url = parse_url($result->data['ObjectURL']);
                 $config = $this->CI->config->item('region');
                 $url = $url['scheme'].'://'.$config['cdn'].$url['path'];
                 return $url;
            } catch (CosException $e) {
                // echo "$e\n";
                return false;
            }
        }
        //分片上传
        try {
            $result = $this->cos_client->upload(
                //bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
                $bucket=$buket,
                $key = $key,
                $body = fopen($body,'rb')
                // $options = array(
                //     "ACL"=>'public-read',
                //     'CacheControl' => 'public-read')
                );
             $url = parse_url($result->data['Location']);
             $config = $this->CI->config->item('region');
             $url = $url['scheme'].'://'.$config['cdn'].$url['path'];
             return $url;
        } catch (\Exception $e) {
           // return echo "$e\n";
           return false;
        }

    }
    /**
     * 删除文件
     * @Author   
     * @DateTime 2018-04-04
     * @version  1.0
     * @param    [type]     $buket [description]
     * @param    [type]     $key   [description]
     * @return   [type]            [description]
     */
    public function deleteFile($buket,$key){
        // 删除COS对象
        try{
            $result = $this->cos_client->deleteObject(array(
                //bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
                'Bucket' => $buket,
                'Key' => $key));
            return true ;
        }catch(\Exception $e) {
            return false;
        }

    }
}

上传图片运用
/**
     * cos上传
     * @Author   
     * @DateTime  2018-04-09
     */
    public function uploadImg(){

        if(!isset($_FILES['file'])) $this->response(200000203,'');

        $scr = $_FILES['file']['tmp_name']; 

        $ext = substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1); // 上传文件后缀

        $dst = md5(time()).'.'.$ext;            //上传文件名称

        $this->load->library('TenUpload');

        $this->config->load('services');

        $config = $this->config->item('region');   //加载COS配置


        $buket = $config['credentials']['buket'];
        $con = $this->config->item('catalog');

        $url = $this->tenupload->uploadFile($buket,$con['images'].$dst,$scr);

        $data = array('url' =>$url);

        $this->response(0,'上传成功',$data);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值