This commit is contained in:
尖叫 2023-11-29 15:39:43 +08:00
parent 7a545ea88f
commit 59f3408b25
2 changed files with 98 additions and 33 deletions

View File

@ -492,50 +492,115 @@ class UploadController extends \app\controllers\Controller
];
return $this->responseHandler($data);
}
$action = "uploadfile";
$fileInfo = $this->uploadFile($action);
if ($rfmt == 'default') {
if ($fileInfo["state"] == "SUCCESS") {
if (empty($_FILES['file']['name'])) {
$data = [
'code' => 1,
'msg' => "请选择要上传的文件"
];
return $this->responseHandler($data);
}
$type = $_FILES['file']['type'];
$explode = explode('/', $type);
$type = end($explode);
// 上传文件的目录
$upload_dir = "/web/uploads/file/";
if (!is_dir($upload_dir)) {
mkdir(\Yii::$app->basePath . '/web/uploads/file', 0777, true);
}
$file = md5($_FILES['file']['name']);
// 上传文件的全名
$upload_file = \Yii::$app->basePath . '/web/uploads/file/' . $file . $type;
// 将上传的文件从临时目录移动到指定目录
if (!move_uploaded_file($_FILES['file']['tmp_name'], $upload_file)) {
$data = [
'code' => 1,
'msg' => "文件上传失败"
];
} else {
$oss = new Oss();
$file = '/web/upload/file/' . $file . '.png';
//上传文件目录
$oss_data = $oss->upload($upload_file, $file);
if ($oss == false) {
$data = [
'code' => 1,
'msg' => "文件上传失败"
];
} else {
$data = [
'code' => 0,
'msg' => 'success',
'data' => [
'url' => $fileInfo['url'],
'extension' => $fileInfo["type"],
'size' => $fileInfo["size"],
'type' => $this->getFileType(trim($fileInfo['type'], "."))
'url' => $oss_data,
]
];
} else {
$data = [
'code' => 1,
'msg' => $fileInfo["state"]
];
}
} else if ($rfmt == 'wangeditor') {
if ($fileInfo["state"] == "SUCCESS") {
$data = [
'errno' => 0,
'message' => 'success',
'data' => [
'url' => $fileInfo['url'],
'alt' => '',
'href' => '',
'extension' => $fileInfo["type"],
'size' => $fileInfo["size"],
'type' => $this->getFileType(trim($fileInfo['type'], "."))
]
];
} else {
$data = [
'errno' => 1,
'message' => $fileInfo["state"]
];
}
}
return $this->responseHandler($data);
}
// //上传文件
// public function actionFile($rfmt = 'default')
// {
// if (!\Yii::$app->request->isPost) {
// $data = [
// 'code' => 1,
// 'msg' => "Invalid Request"
// ];
// return $this->responseHandler($data);
// }
//
//
// $action = "uploadfile";
// $fileInfo = $this->uploadFile($action);
// if ($rfmt == 'default') {
// if ($fileInfo["state"] == "SUCCESS") {
// $data = [
// 'code' => 0,
// 'msg' => 'success',
// 'data' => [
// 'url' => $fileInfo['url'],
// 'extension' => $fileInfo["type"],
// 'size' => $fileInfo["size"],
// 'type' => $this->getFileType(trim($fileInfo['type'], "."))
// ]
// ];
// } else {
// $data = [
// 'code' => 1,
// 'msg' => $fileInfo["state"]
// ];
// }
// } else if ($rfmt == 'wangeditor') {
// if ($fileInfo["state"] == "SUCCESS") {
// $data = [
// 'errno' => 0,
// 'message' => 'success',
// 'data' => [
// 'url' => $fileInfo['url'],
// 'alt' => '',
// 'href' => '',
// 'extension' => $fileInfo["type"],
// 'size' => $fileInfo["size"],
// 'type' => $this->getFileType(trim($fileInfo['type'], "."))
// ]
// ];
// } else {
// $data = [
// 'errno' => 1,
// 'message' => $fileInfo["state"]
// ];
// }
// }
// return $this->responseHandler($data);
// }
//上传视频
public function actionVideoFile()
{

Binary file not shown.