PHP阿里云oss图片上传以及封装上传类

阿里云上传类

<?php
USE OSS\OssClient;
USE OSS\Core\OssException;

require "OssUpload/autoload.php";

Class AliUpload{

    public function __construct(){
        $this->init();
    }

    public function init(){
        $CI                 = &get_instance();
        $CI->config->load('oss', TRUE);
        $config = $CI->config->item('oss','oss');   //加载oss配置
        $this->Access_Key   = $config['accessKeyId'];
        $this->Secret_Key   = $config['accessKeySecret'];
        $this->bucket       = $config['bucketName'];
        $this->endpoint     = $config['endpoint'];

    }

    /**
     * 上传接口
     * @Author   
     * @DateTime  2017-03-08
     * @param string $bucket bucket name
     * @param string $object object name
     * @param string $file local file path
     * @param array $options
     * @throws OssException  [文件类型]
     */
    public function upload($dst,$src){
        //获取对象
        $auth = new OssClient($this->Access_Key,$this->Secret_Key,$this->endpoint);
         try {
            //上传图片
            $result  = $auth->uploadFile($this->bucket,$dst,$src);
            return $result['info']['url'];
         } catch (OssException $e) {
            return $e->getMessage();
         }
    }

    /**
     * @Author   
     * @DateTime  2018-03-08
     * @return    [type]      [description]
     */
    public function uploadVideo($dst,$src){
        $ossClient = new OssClient($this->Access_Key,$this->Secret_Key,$this->endpoint);
        try{
            $result = $ossClient->multiuploadFile($this->bucket,$dst,$src);
            return $result['info']['url'];
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ":  OK" . "\n");

    }


}

oss上传图片

/**
   * 上传图片
   * @Author   
   * @DateTime  2018-03-10
   * @return    [json]      [图片url]
   */
    public function uploadImg(){

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

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

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

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

      $url = $this->aliupload->upload($dst,$scr);

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

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

猜你喜欢

转载自blog.csdn.net/jerryyang_2017/article/details/80326960