251 lines
8.9 KiB
PHP
Executable File
251 lines
8.9 KiB
PHP
Executable File
<?php
|
||
|
||
namespace app\admin\controller;
|
||
|
||
// require_once '../extend/oss/autoload.php';
|
||
|
||
use app\admin\controller\Base;
|
||
use OSS\Core\OssException;
|
||
use OSS\OssClient;
|
||
use app\common\server\TencentCosUploader;
|
||
use think\facade\Db;
|
||
use think\facade\Filesystem;
|
||
use think\facade\Request;
|
||
use Qcloud\Cos\Client;
|
||
class Upload extends Base
|
||
{
|
||
|
||
private $uploader;
|
||
|
||
public function initialize()
|
||
{
|
||
|
||
$config = getConfig('uploads');
|
||
$this->uploader = new TencentCosUploader($config);
|
||
}
|
||
|
||
|
||
/**
|
||
* 上传文件到腾讯云 COS
|
||
*/
|
||
public function picture()
|
||
{
|
||
// 获取表单上传文件
|
||
$files = request()->file('file', '');
|
||
if (empty($files)) {
|
||
return $this->renderError("请上传图片");
|
||
}
|
||
|
||
// 文件格式校验
|
||
$ext = ['jpg', 'png', 'jpeg', 'JPG', 'PNG', 'JPEG', 'gif', 'apk', 'mp3'];
|
||
$type = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.') + 1);
|
||
if (!in_array($type, $ext)) {
|
||
return $this->renderError("文件格式错误");
|
||
}
|
||
|
||
// 文件大小校验
|
||
if ($_FILES['file']['size'] > 20971520) {
|
||
return $this->renderError("上传文件不能超过20M");
|
||
}
|
||
|
||
// 获取存储目录参数,如果没有则使用默认目录
|
||
$dir = request()->param('dir', 'topic');
|
||
|
||
// 判断是否上传过
|
||
$hash = $files->hash('sha1');
|
||
try {
|
||
// 上传文件到腾讯云 COS
|
||
$uploadResult = $this->uploader->uploadFilePath($_FILES['file']['tmp_name'], $type, $dir);
|
||
$imgurl = $uploadResult['full_url'];
|
||
// 新增数据到数据库
|
||
$save_data['token'] = $hash;
|
||
$save_data['imgurl'] = $imgurl; // 保存 COS 中的文件路径
|
||
$save_data['addtime'] = time();
|
||
$save_data['type'] = 1;
|
||
$res = Db::name('picture')->insertGetId($save_data);
|
||
if ($res) {
|
||
$data['id'] = $res;
|
||
$data['path'] = $imgurl; // 返回文件的完整访问 URL
|
||
$data['imgurl'] = $imgurl;
|
||
return $this->renderSuccess('上传成功', $data);
|
||
} else {
|
||
return $this->renderError('上传失败');
|
||
}
|
||
} catch (\Exception $e) {
|
||
return $this->renderError('文件上传失败:' . $e->getMessage());
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 上传文件
|
||
*/
|
||
public function picture_file()
|
||
{
|
||
// 获取表单上传文件
|
||
$files = request()->file('file', '');
|
||
if (empty($files)) {
|
||
return $this->renderError("请上传图片");
|
||
}
|
||
|
||
$ext = ['jpg', 'png', 'jpeg', 'JPG', 'PNG', 'JPEG', 'gif', 'apk', 'mp3'];
|
||
$type = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.') + 1);
|
||
if (!in_array($type, $ext)) {
|
||
return $this->renderError("文件格式错误");
|
||
}
|
||
if ($_FILES['file']['size'] > 20971520) {
|
||
return $this->renderError("上传文件不能超过20M");
|
||
}
|
||
|
||
// 判断是否上传过
|
||
$hash = $files->hash('sha1');
|
||
$info = Db::name('picture')->where('token', $hash)->find();
|
||
|
||
if ($info) {
|
||
// if (false) {
|
||
$data['path'] = imageUrl($info['imgurl']);
|
||
$data['imgurl'] = $info['imgurl'];
|
||
return $this->renderSuccess('上传成功', $data);
|
||
} else {
|
||
// 保存图片
|
||
$date = date('Ymd');
|
||
$uniqueFileName = md5(uniqid(rand(), true)) . '.' . $type;
|
||
$saveDir = './storage/topic/' . $date;
|
||
if (!is_dir($saveDir)) {
|
||
mkdir($saveDir, 0777, true);
|
||
}
|
||
$savename = $saveDir . '/' . $uniqueFileName;
|
||
// 移动文件到目标目录
|
||
if (move_uploaded_file($_FILES['file']['tmp_name'], $savename)) {
|
||
$savename = str_replace('\\', '/', $savename);
|
||
$savename = substr($savename, 1); // 去掉开头的 "."
|
||
|
||
// 新增数据
|
||
$save_data['token'] = $hash;
|
||
$save_data['imgurl'] = $savename;
|
||
$save_data['addtime'] = time();
|
||
$res = Db::name('picture')->insertGetId($save_data);
|
||
if ($res) {
|
||
$data['id'] = $res;
|
||
$data['path'] = imageUrl($savename);
|
||
$data['imgurl'] = $savename;
|
||
return $this->renderSuccess('上传成功', $data);
|
||
} else {
|
||
return $this->renderError('上传失败');
|
||
}
|
||
} else {
|
||
return $this->renderError('文件保存失败');
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 阿里云oss上传
|
||
*/
|
||
public function picture_other()
|
||
{
|
||
#获取表单上传文件
|
||
$files = request()->file('file', '');
|
||
if (empty($files)) {
|
||
return $this->renderError("请上传文件");
|
||
}
|
||
$ext = ['apk'];
|
||
$type = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.') + 1);
|
||
if (!in_array($type, $ext)) {
|
||
return $this->renderError("文件格式错误");
|
||
}
|
||
if ($_FILES['file']['size'] > 104857600) {
|
||
return $this->renderError("上传文件不能超过100M");
|
||
}
|
||
$oss = Config::getConfigInfo('aliyun_oss');
|
||
$object_file = 'newimage';
|
||
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
|
||
$accessKeyId = $oss['file_accessKeyId'];
|
||
$accessKeySecret = $oss['file_accessKeySecret'];
|
||
// Endpoint以杭州为例,其它Region请按实际情况填写。
|
||
$endpoint = $oss['file_endpoint'];
|
||
// 设置存储空间名称。
|
||
$bucket = $oss['file_bucket'];
|
||
// 设置文件名称。
|
||
$object = $object_file . '/' . date('Ym') . '/' . sha1(date('YmdHis', time()) . uniqid()) . '.' . $type;
|
||
// <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
|
||
$filePath = $_FILES['file']['tmp_name'];
|
||
try {
|
||
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
|
||
$ossClient->uploadFile($bucket, $object, $filePath);
|
||
} catch (OssException $e) {
|
||
return $this->renderError("上传失败");
|
||
}
|
||
$data['path'] = imageUrl($object);
|
||
$data['imgurl'] = $object;
|
||
return $this->renderSuccess('上传成功', $data);
|
||
}
|
||
|
||
|
||
/**
|
||
* 上传图片
|
||
*/
|
||
public function picture_old()
|
||
{
|
||
#获取表单上传文件
|
||
$files = request()->file('file', '');
|
||
if (empty($files)) {
|
||
return $this->renderError("请上传图片");
|
||
}
|
||
$ext = ['jpg', 'png', 'jpeg', 'JPG', 'PNG', 'JPEG', 'gif'];
|
||
$type = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.') + 1);
|
||
if (!in_array($type, $ext)) {
|
||
return $this->renderError("文件格式错误");
|
||
}
|
||
if ($_FILES['file']['size'] > 5242880) {
|
||
return $this->renderError("上传文件不能超过5M");
|
||
}
|
||
#判断是否上传过
|
||
$hash = $files->hash('sha1');
|
||
$info = Picture::where('token', $hash)->find();
|
||
if ($info) {
|
||
$data['id'] = $info['id'];
|
||
$data['path'] = imageUrl($info['imgurl']);
|
||
$data['imgurl'] = $info['imgurl'];
|
||
return $this->renderSuccess('上传成功', $data);
|
||
} else {
|
||
#保存图片
|
||
$savename = \think\facade\Filesystem::disk('public')->putFile('topic', $files);
|
||
$hash = $files->hash('sha1');
|
||
$savename = '/storage/' . $savename;
|
||
$savename = str_replace('\\', '/', $savename);
|
||
#新增数据
|
||
$save_data['token'] = $hash;
|
||
$save_data['imgurl'] = $savename;
|
||
$save_data['addtime'] = time();
|
||
$res = (new Picture())->save($save_data);
|
||
if ($res) {
|
||
$data['id'] = $res;
|
||
$data['path'] = imageUrl($savename);
|
||
$data['imgurl'] = $savename;
|
||
return $this->renderSuccess('上传成功', $data);
|
||
} else {
|
||
return $this->renderError('上传失败');
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 上传证书
|
||
*/
|
||
public function wechat_cert()
|
||
{
|
||
$files = request()->file('file', '');
|
||
$ext = ['cer', 'pfx'];
|
||
$type = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.') + 1);
|
||
if (!in_array($type, $ext)) {
|
||
return $this->renderError("文件格式错误");
|
||
}
|
||
$savename = $_FILES['file']['name'];
|
||
\think\facade\Filesystem::disk('ssl')->putFileAs('', $files, $savename);
|
||
return $this->renderSuccess('上传成功', $savename);
|
||
|
||
}
|
||
}
|