打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
oss 阿里上传
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();
    }
}   
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JEESZ分布式架构集成阿里云OSS存储
OSS —— 对象存储介绍
群晖+阿里云DDNS动态IP访问
thinkphp集成系列之阿里云oss
[PHP]阿里云短信服务接口类
阿里云短信验证码发送类
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服