use OSS\OssClient;
use OSS\Core\OssException;
/**
* [local_upload 上传到本地服务器]
* @author 984270382@qq.com
* @copyright passboat
* @return [type] [description]
*/
function local_upload($file){
$path = 'public/uploads/'.date('Ymd/');
if(!file_exists($path)){
mkdir ($path,0777,true);
}
$name = time().$file['name'];
$statu = strtolower(substr($name,strrpos($name,'.')+1));
$allow_type = array('jpg','jpeg','gif','png');
if(!in_array($statu, $allow_type)){
return ['code'=>-1,'msg'=>'图片格式错误'];
}
if($file['size']/1024/1024 > 5){
return ['code'=>-1,'msg'=>'图片大小超过5M!'];
}
if(move_uploaded_file($file['tmp_name'],$path.'/'.$name)){
return ['path'=>$path,'name'=>$name];
}else{
return false;
}
}
/**
* [ali_upload oss阿里上传]
* @author 984270382@qq.com
* @copyright passboat
* @param [type] $file [description]
* @return [type] [description]
*/
function ali_upload($file){
// 获取配置 信息
$aliData = [/* */];
$accessKeyId = $aliData['accesskeyid'] ;
$accessKeySecret = $aliData['accesskeysecret'];
$endpoint = $aliData['endpoint'];
$bucket= $aliData['bucket'];
try {
// 上传到本地服务器失败
$res = local_upload($file);
if($res !== false){
$path = $res['path'];
$name = $res['name'];
$filePath = $path.$name;
}else{
return ['code'=>-1,'上传本地失败'];
}
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
$ossClient->putObject($bucket, $filePath, file_get_contents(ROOT_PATH.$filePath));
return ['code'=>1,'url'=>$info['url']];
} catch (OssException $e) {
return $e->getMessage();
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。