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
- php关于阿里云OSS简易操作类
- java 操作阿里云 OSS
- Java操作阿里云OSS系统工具类
- PHP 阿里云oss sdk详细介绍
- Java操作阿里云OSS系统
- Java操作阿里云OSS系统配置文件
- 阿里云OSS文件下载功能简易实现
- 阿里云OSS上传文件工具类
- 阿里云OSS开发
- 阿里云OSS使用方法
- 测试阿里云OSS
- 阿里云 OSS+CDN
- 阿里云oss
- 阿里云OSS
- 阿里云oss
- 阿里云OSS配置
- 阿里云OSS术语
- PHP调用接口到阿里云OSS同步上传图片
- 黑马程序员——Java基础---面向对象(封装、继承、多态)
- css进阶学习网站
- 转:C#中base关键字的几种用法
- hdu 3681 Prison Break bfs + 二分 + TSP
- 乐讯机器人高级版【版本认证20151015】
- php关于阿里云OSS简易操作类
- C/C++中函数参数传之指针形参
- [20151015]SCZ训练
- 数据本地化存储之NSUserdefaults,plist文件,归档解档,数据库sqlite(FMDB)
- java 内部类如何获取外部类引用
- thinkphp 切换数据库的方法
- 30分钟了解C++11新特性
- 用层次分析法解决手机选择困难症
- 蓝懿ioS 技术内容和心得 10.15