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

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

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


<?php
require_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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值