php关于阿里云OSS简易操作类

来源:互联网 发布:笔电锋淘宝 编辑:程序博客网 时间:2024/06/05 02:39

首先要下载官司方的php-SDK包

地址:https://docs.aliyun.com/?spm=5176.383663.9.7.DcfZ6d#/pub/oss/sdk/sdk-download&php


<?phprequire_once 'oss/alioss.class.php';require_once 'oss/util/oss_util.class.php';/** * Author: Hancock * Email: 84622365@qq.com * Date: 10/15/2015 * Time: 9:48 AM * Des:阿里云 OSS操作类 */class oss extends ALIOSS{    private $bucket="Your Bucket Name";    private $object;    private $size;    var $file;    /**     * 上传文件     * @param $file     * @param string $object     * @param string $type     * @return array     */    public function upload($file, $object='',$type='image'){        $this->file = $file;        $this->object = $object;        if ($this->file["error"] > 0)        {            return $this->msg(false, $this->file["error"]);        }        if(!is_file($this->file['tmp_name'])){            return $this->msg(false, '文件不存在');        }        switch($type){            case 'image':                return $this->uploadImage();                break;            default:                break;        }    }    /**     * 上传图片     * @return array     */    public function uploadImage(){        if((!$this->imageType($this->file['type']))||(!$this->imageSize($this->file['size']))){            return $this->msg(false, '该文件不是图片或者超最大文件大小限制');        }        $content = '';        $length = 0;        $fp = fopen($this->file["tmp_name"],'r');        if($fp)        {            $f = fstat($fp);            $length = $f['size'];            while(!feof($fp))            {                $content .= fgets($fp,8192);            }        }        $upload_file_options = array('content' => $content, 'length' => $length);        $res = $this->upload_file_by_content($this->bucket, 'test-new/'.$this->file["name"], $upload_file_options);        if($res->status==200){            return $this->msg(true);        } else{            return $this->msg(false, 'oss code:'.$res->status);        }    }    /**     * 上传文件     * @param $file     * @return array     * @throws OSS_Exception     */    public function uploadFile($file){        $file = trim($file,'/');        $this->file = dirname(__DIR__).'/'.$file;        if(!is_file($this->file)){            return $this->msg(false,'fail'.$file);        }        $res = $this->upload_file_by_file($this->bucket,$file,$this->file);        if($res->status==200){            return $this->msg(true,$file);        } else{            return $this->msg(false, 'oss code:'.$res->status);        }    }    /**     * 列出当前目录object     * @param string $options     * @return array     */    public function list_object($options){        return OSSUtil::parse_response(parent::list_object($this->bucket,$options));    }    /**     * 删除object     * @param string $object     * @param string $options     * @return array     */    public function delete_object($object, $options){        return OSSUtil::parse_response(parent::delete_object($this->bucket, $object, $options));    }    /**     * 判断是否是图片     * @param $type     * @return bool     */    private function imageType($type){        $imageType = array();        $imageType[] = 'image/gif';        $imageType[] = 'image/jpeg';        $imageType[] = 'image/pjpeg';        $imageType[] = 'image/png';        if(in_array($type, $imageType)){            return true;        }else{            return false;        }    }    /**     * 图片大小限制     * @param $size     * @return bool     */    private function imageSize($size){        $this->size = 5242880;//5M        if($size>$this->size){            return false;        }else{            return true;        }    }    /**     * 返回信息     * @param $status     * @param string $msg     * @return array     */    private function msg($status, $msg=''){        return array('status'=>$status,'msg'=>trim($msg));    }    /**     * 获取文件     * @param $response     * @return array     */    private function getObjectsByBody($response){        $objects = $response['body']['ListBucketResult']['Contents'];        $data = array();        if(array_key_exists('Key',$objects)){            $data[] = $objects['Key'];        }else {            foreach ($objects as $value) {                $data[] = $value['Key'];            }        }        return $data;    }    /**     * 获取文件夹     * @param $response     * @return array     */    private function getPrefixByBody($response){        $prefix = $response['body']['ListBucketResult']['CommonPrefixes'];        $data = array();        if(sizeof($prefix)==1){            $data[] = $prefix['Prefix'];        }else{            foreach($prefix as $value){                $data[] = $value['Prefix'];            }        }        return $data;    }    /**     * 遍历bucket获取所有object     * @param $options     * @param array $data     * @return array     */    public function getAllObjects($options, $data = array()){        $res = $this->list_object($options);        $objects = $this->getObjectsByBody($res);        if(sizeof($objects)>0) {            foreach ($objects as $value) {                $data[] = $value;            }        }        $prefix = $this->getPrefixByBody($res);        if(sizeof($prefix)>0){            foreach($prefix as $value){                $data[] = $value;                $sub_options = array(                    'prefix' => $value                );                $data = $this->getAllObjects($sub_options, $data);            }        }        return $data;    }    /**     * 删除文件夹下所有文件     * @param $prefix     * @return bool     */    public function delByPrefix($prefix){        $options=array(            'prefix' => $prefix        );        $objects = array_reverse($this->getAllObjects($options));        foreach($objects as $object){            $this->delete_object($object);        }        return true;    }}


0 0
原创粉丝点击