本文实例讲述了php版阿里云OSS图片上传类。分享给大家供大家参考,具体如下:
1.阿里云基本函数
ossClient->putObject($this->bucket,$object,$content,$options);
} catch (OssException $e) {
return $e->getMessage();
}
return TRUE;
}
/**
* 上传指定的本地文件内容
*/
public function uploadFile($imgPath,$object) //$_FILES[‘img’][‘tmp_name’]
{
$filePath = $imgPath;
$options = array();
try {
$this->ossClient->uploadFile($this->bucket,$filePath,$options);
} catch (OssException $e) {
return $e->getMessage();
}
return TRUE;
}
// 删除对象
public function deleteObject($object) {
try {
$this->ossClient->deleteObject($this->bucket,$object);
} catch (OssException $e) {
return $e->getMessage();
}
return TRUE;
}
// 判断对象是否存在
public function doesObjectExist($object) {
try {
$result = $this->ossClient->doesObjectExist($this->bucket,$object);
} catch (OssException $e) {
return $e->getMessage();
}
return $result;
}
// 批量删除对象
public function deleteObjects($objects) {
try {
$this->ossClient->deleteObjects($this->bucket,$objects);
} catch (OssException $e) {
return $e->getMessage();
}
return TRUE;
}
/**
* 获取object的内容
*
* @param OssClient $ossClient OssClient实例
* @param string $bucket 存储空间名称
* @return null
*/
public function getObject($object)
{
$options = array();
try {
$content = $this->ossClient->getObject($this->bucket,$options);
} catch (OssException $e) {
return $e->getMessage();
}
// file_get_contents
return $content;
}
2.基本配置与辅助函数
-1,// 上传文件的最大值
‘supportMulti’ => true,// 是否支持多文件上传
‘allowExts’ => array(),// 允许上传的文件后缀 留空不作后缀检查
‘allowTypes’ => array(),// 允许上传的文件类型 留空不做检查
‘thumb’ => false,// 使用对上传图片进行缩略图处理
‘imageClassPath’ => ‘ORG.Util.Image’,// 图库类包路径
‘thumbMaxWidth’ => ”,// 缩略图最大宽度
‘thumbMaxHeight’ => ”,// 缩略图最大高度
‘thumbPrefix’ => ‘thumb_’,// 缩略图前缀
‘thumbSuffix’ => ”,’thumbPath’ => ”,// 缩略图保存路径
‘thumbFile’ => ”,// 缩略图文件名
‘thumbExt’ => ”,// 缩略图扩展名
‘thumbRemoveOrigin’ => false,// 是否移除原图
‘zipImages’ => false,// 压缩图片文件上传
‘autoSub’ => false,// 启用子目录保存文件
‘subType’ => ‘hash’,// 子目录创建方式 可以使用hash date custom
‘subDir’ => ”,// 子目录名称 subType为custom方式后有效
‘dateFormat’ => ‘Ymd’,’hashLevel’ => 1,// hash的目录层次
‘savePath’ => ”,// 上传文件保存路径
‘autoCheck’ => true,// 是否自动检查附件
‘uploadReplace’ => false,// 存在同名是否覆盖
‘saveRule’ => ‘uniqid’,// 上传文件命名规则
‘hashType’ => ‘md5_file’,// 上传文件Hash规则函数名
);
// 错误信息
private $error = ”;
// 上传成功的文件信息
private $uploadFileInfo ;
public function __get($name){
if(isset($this->configinfo[$name])) {
return $this->configinfo[$name];
}
return null;
}
public function __set($name,$value){
if(isset($this->configinfo[$name])) {
$this->configinfo[$name] = $value;
}
}
public function __isset($name){
return isset($this->configinfo[$name]);
}
/**
* 架构函数
* @access public
* @param array $config 上传参数
*/
public function __construct($config=array()) {
if(is_array($config)) {
$this->config = array_merge($this->config,$config);
}
$this->bucket = C(‘OSS_TEST_BUCKET’);
$this->ossClient = new OssClient(C(‘OSS_ACCESS_ID’),C(‘OSS_ACCESS_KEY’),C(‘OSS_ENDPOINT’),false);
}
3.主函数
savePath;
}
$fileInfo = array();
$isUpload = false;
// 获取上传的文件信息
// 对$_FILES数组信息处理
$files = $this->dealFiles($_FILES);
foreach($files as $key => $file) {
//过滤无效的上传
if(!empty($file[‘name’])) {
//登记上传文件的扩展信息
if(!isset($file[‘key’])) $file[‘key’] = $key;
$file[‘extension’] = $this->getExt($file[‘name’]);
$file[‘savepath’] = $savePath;
$file[‘savename’] = $this->getSaveName($file);
// 自动检查附件
if($this->autoCheck) {
if(!$this->check($file))
return false;
}
//保存上传文件
if(!$this->save($file)) return false;
if(function_exists($this->hashType)) {
$fun = $this->hashType;
$file[‘hash’] = $fun($this->autoCharset($file[‘savepath’].$file[‘savename’],’utf-8′,’gbk’));
}
//上传成功后保存文件信息,供其他地方调用
unset($file[‘tmp_name’],$file[‘error’]);
$fileInfo[] = $file;
$isUpload = true;
}
}
if($isUpload) {
$this->uploadFileInfo = $fileInfo;
return true;
}else {
$this->error = ‘没有选择上传文件’;
return false;
}
}
4.核心处理函数