diff --git a/modules/file/controllers/UploadController.php b/modules/file/controllers/UploadController.php index 1532302..f382660 100644 --- a/modules/file/controllers/UploadController.php +++ b/modules/file/controllers/UploadController.php @@ -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() { diff --git a/web/uploads/file/588e8d87b845713e8ac5df30269a908bmp4 b/web/uploads/file/588e8d87b845713e8ac5df30269a908bmp4 new file mode 100644 index 0000000..5b0332b Binary files /dev/null and b/web/uploads/file/588e8d87b845713e8ac5df30269a908bmp4 differ