layui结合阿里云OSS实现图片上传

场景:微信用户图片上传使用第三方阿里云存储,方便系统的运行和节省系统的空间

1.购买阿里云OSS服务,详情请查看我的另外一篇博客

2.layui前端代码


    奖品图片:
    
        
建议尺寸640*640
//js代码






后端处理代码

1.引入相应的OSS工具sdk

 0 &&
                $files['size'] < 100 * 1024 * 1024)
            {
                $extArray = explode('.',$files['name']);
                $fileExt = $extArray[count($extArray) -1];
                $filename = md5(time()) .rand(11,99). '.' . $fileExt;
                //$filename此处随机文件名
                if(move_uploaded_file($files['tmp_name'], $tosrc.$filename))
                {
                    //成功,返回完整的图片地址
                    return  $web.$filename;
                }
                return false;
            }
            return false;
        }
        return false;
    }


    //执行上传
    public function ActionLoadUpload()
    {
        if(!isset($_FILES['file']) || empty($_FILES['file']['tmp_name']) || $_FILES['file']['error']!=0)
        {
            return array('code'=>1,'msg'=>'请选择正确的图片');
        }

        oss_Common::createBucket();
//        return['code'=>0,'msg'=>'y'];
        $bucketName = oss_Common::getBucketName();
        $ossClient = oss_Common::getOssClient();
        $filePath = __FILE__;
        $_return=$ossClient->uploadFile($bucketName,SAVE_IMG_LARGER.'/games_wheel/'.time().'.png',$_FILES['file']['tmp_name']);
        $file='';
        if (!empty($_return['oss-request-url']))
        {
            //正则表达式
            $reg = '/(http):\/\/([^\/]+)/i';
            preg_match($reg,$_return['oss-request-url'],$res);
            $file=preg_replace($reg,SOURCE_URL,$_return['oss-request-url']);
        }
        if($file=='')
        {
            return array('code'=>1,'msg'=>'上传失败');
        }
        return array('code'=>0,'msg'=>'上传成功','file'=>$file);
    }

    protected function uploadFile()
    {
        //表单上传
        if (!is_uploaded_file($_FILES['file']['tmp_name'])){
            return array('code'=>1,'msg'=>'上传内容为空');
        }
        $allow_type = array('image/gif', 'image/gif','image/jpg', 'image/jpeg', 'image/png', 'image/bmp');
        if (!isset($_FILES['file']['type']) || !in_array(strtolower($_FILES['file']['type']),$allow_type)){
            return array('code'=>1,'msg'=>'上传类型不合法,'.$_FILES['file']['type']);
        }
        if ($_FILES['file']['error'] > 0 || $_FILES['file']['size'] > 1024 * 1024 * 5){
            return array('code'=>1,'msg'=>'图片错误或者超过5M大小');
        }
        if (empty($this->data['file_dir'])){
            return ReturnError('上传路径错误');
        }
        $oss_path = SAVE_IMG_LARGER.'/'.trim($this->data['file_dir']).'/'.md5($_FILES['file']['name']).'.png';
        return $this->saveToOss($_FILES['file']['tmp_name'],$oss_path);
    }

    /**
     * 上传文件到OSS存储
     * @param $source :本地文件,如$_FILES['file']['tmp_name']
     * @param $toPath :阿里云oss保存的位置,地址开头不能带有‘/’,正确路径:“img/abc/xxx.jpg”
     * @return array
     */
    private function saveToOss($source_file,$toOssPath)
    {
        oss_Common::createBucket();
        $bucketName = oss_Common::getBucketName();
        $ossClient = oss_Common::getOssClient();
        $_return=$ossClient->uploadFile($bucketName,$toOssPath,$source_file);
        $file='';
        if (!empty($_return['oss-request-url'])){
            //正则表达式
            $reg = '/(http):\/\/([^\/]+)/i';
            preg_match($reg,$_return['oss-request-url'],$res);
            $file = preg_replace($reg,SOURCE_URL,$_return['oss-request-url']);
        }
        if(empty($file)){
            return array('code'=>1,'msg'=>'上传失败');
        }
        return array('code'=>0,'msg'=>'上传成功','url'=>$file);
    }
}

你可能感兴趣的:(php)