正常情况下使用七牛云存储
use Qiniu\Storage\UploadManager; // 七牛上传use Qiniu\Auth; /*
@name 七牛云上传接口
@param file 文件
@return文件地址【绝对地址】
*/
public function upQiniouOne()
{
$file = $_FILES['file'];
if (empty($file)) {
throw new Exception('没有文件被上传', 10002);
}
// 生成文件名
$str = explode('.',$file['name']);
$ext = $str; // 获取后缀名
$filetype = ['jpg', 'jpeg', 'gif', 'bmp', 'png'];
if (!in_array( $ext, $filetype))
{
dowjson(1,"请上传图片类型的文件");
}
$user = $this->useinfo;
// 上传到七牛后保存的文件名
$key = $user->userid . date('YmdHis') . rand(0, 9999) . '.' . $ext;
// 获得7牛云配置信息
$accessKey = Config::get('qiniu.accessKey');
$secretKey = Config::get('qiniu.secretKey');
$bucket = Config::get('qiniu.bucket');
$upManager = new UploadManager();
$auth = new Auth($accessKey, $secretKey);
$token = $auth->uploadToken($bucket);
// 上传token,文件名,文件
list($ret, $error) = $upManager->putFile($token, $key, $file['tmp_name']);
if ($error !== null) {
downjson(1,"上传出错");
throw new Exception('上传出错'.serialize($error));
}
$ret['key'] = 'http://'.Config::get("qiniu.DOMAIN").'/'.$ret['key'];
downjson(0,"返回成功",1,$ret);
}
页:
[1]