womao
//接收文件上传的值
$file = $_FILES;
//本地的文件路径
$localFilePath = $file[\'file\'][\'tmp_name\'];
//截取文件后缀名
//$suffix = \'.jpg\';
$suffix = strtolower(substr($file[\'file\'][\'name\'],strpos($file[\'file\'][\'name\'],\'.\')));
//生成一个唯一的文件名称,重命名 (md5加密原文件名+秒+随机数)
$fileName = md5($file[\'file\'][\'name\']) . date(\'s\',time()) . rand(1,9999999);
$fileName .= $suffix;
//上传七牛云业务逻辑
$accessKey = \'\'; //去控制台的秘钥管理拿AK
$secretKey = \'\';//去控制台的秘钥管理拿SK
//七牛云桶名,根据自己实际进行填写
$bucket = \'\';
//图片违规验证
$url = \'http://ai.qiniuapi.com/v3/image/censor\';
$method="POST";
$host="ai.qiniuapi.com";
$contentType = \'application/json\';
//图片地址
$image_url=\'https://www.gaopinimages.com/img/newLogo_back_white.bd2c2e64.png\';
$scenes = [
\'censor\' => [\'pulp\', \'terror\', \'politician\', \'ads\'],
\'pulp\' => [\'pulp\'],
\'terror\' => [\'terror\'],
\'politician\' => [\'politician\'],
\'ads\' => [\'ads\']
];

$body = [
\'data\' => [\'uri\' => $image_url],
\'params\' => [\'scenes\' => $scenes[\'censor\']]
];
$auth = new Auth($accessKey, $secretKey);

$headers=$auth->authorizationV2($url,$method,json_encode($body),$contentType);
$headers[\'Content-type\']=$contentType;
$headers[\'Host\']=$host;

$response=Client::post($url,json_encode($body),$headers);

// 生成上传Token
$token = $auth->uploadToken($bucket);

// 构建 UploadManager 对象
$uploadMgr = new UploadManager();

// 调用 UploadManager 的 putFile 方法进行文件的上传。
list($ret, $err) = $uploadMgr->putFile($token, $fileName, $localFilePath);

//错误信息提示
if ($err != null) {
//可调整为错误页面
$this->error(\'上传文件失败\');
}

//把七牛云图片路径存储到我们自己的数据库 七牛云图片路径
$imageUrl = \'http://qtpud69oi.hn-bkt.clouddn.com/\' . $fileName;
//入库业务逻辑 create save 只有这两个模型方法才能自动写入时间戳
$result =BannerModel::create([
\'image_url\'=>$imageUrl,
]);

$this->success(\'上传文件成功\');

分类:

技术点:

相关文章: