"临时文件错误", "ERROR_TMP_FILE_NOT_FOUND" => "找不到临时文件", "ERROR_SIZE_EXCEED" => "文件大小超出网站限制", "ERROR_TYPE_NOT_ALLOWED" => "文件类型不允许", "ERROR_CREATE_DIR" => "目录创建失败", "ERROR_DIR_NOT_WRITEABLE" => "目录没有写权限", "ERROR_FILE_MOVE" => "文件保存时出错", "ERROR_FILE_NOT_FOUND" => "找不到上传文件", "ERROR_WRITE_CONTENT" => "写入文件内容错误", "ERROR_UNKNOWN" => "未知错误", "ERROR_DEAD_LINK" => "链接不可用", "ERROR_HTTP_LINK" => "链接不是http链接", "ERROR_HTTP_CONTENTTYPE" => "链接contentType不正确", "INVALID_URL" => "非法 URL", "INVALID_IP" => "非法 IP" ); public function checkToken($token): bool { $token2 = date("Ym-d") . "$@."; Log::info(base64_encode($token2), [$token]); return base64_encode($token2) == $token; } public function upload(Request $request) { if (!$this->checkToken($request->input("token"))) return $this->failure("upload token失效"); $action = $request->action; $config = config('UEditorUpload.upload'); switch ($action) { case 'config': return Response()->json($config) ->header('Access-Control-Allow-Headers', '*') ->setCallback(request()->input('callback')); //->header("Content-Type", "text/html; charset=utf-8"); case 'uploadimage': $upload_config = array( "pathFormat" => $config['imagePathFormat'], "maxSize" => $config['imageMaxSize'], "allowFiles" => $config['imageAllowFiles'] ); $field = $config['imageFieldName']; break; case 'uploadscrawl': $upload_config = array( "pathFormat" => $config['scrawlPathFormat'], "maxSize" => $config['scrawlMaxSize'], "allowFiles" => $config['scrawlAllowFiles'], "oriName" => "scrawl.png" ); $field = $config['scrawlFieldName']; break; case 'uploadvideo': $upload_config = array( "pathFormat" => $config['videoPathFormat'], "maxSize" => $config['videoMaxSize'], "allowFiles" => $config['videoAllowFiles'] ); $field = $config['videoFieldName']; break; case 'uploadfile': default: $upload_config = array( "pathFormat" => $config['filePathFormat'], "maxSize" => $config['fileMaxSize'], "allowFiles" => $config['fileAllowFiles'] ); $field = $config['fileFieldName']; break; } $file = $request->file($field); $oriName = $file->getClientOriginalName(); $fileSize = $file->getSize(); $fileType = $file->getClientOriginalExtension(); $filePath = $file->getRealPath(); $fileName = $file->getFileName(); $result = [ 'original' => $oriName, 'size' => $fileSize, 'state' => "SUCCESS", 'title' => $fileName, 'type' => $fileType, 'url' => $filePath, ]; if (!$file) { $result['stateInfo'] = $this->getStateInfo("ERROR_FILE_NOT_FOUND"); return Response()->json($result) ->header('Access-Control-Allow-Headers', '*'); //->header("Content-Type", "text/html; charset=utf-8"); } if (!file_exists($filePath)) { $result['stateInfo'] = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND"); return Response()->json($result) ->header('Access-Control-Allow-Headers', '*'); //->header("Content-Type", "text/html; charset=utf-8"); } else if (!in_array('.' . $file->getClientOriginalExtension(), $upload_config['allowFiles'])) { $result['stateInfo'] = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED"); return Response()->json($result) ->header('Access-Control-Allow-Headers', '*'); //->header("Content-Type", "text/html; charset=utf-8"); } else if ($fileSize > $upload_config['maxSize']) { $result['stateInfo'] = $this->getStateInfo("ERROR_SIZE_EXCEED"); return Response()->json($result) ->header('Access-Control-Allow-Headers', '*'); //->header("Content-Type", "text/html; charset=utf-8"); } $url = UploadService::uploadFile($file); if ($url) { $result['url'] = $url; $this->stateInfo = $this->stateMap[0]; } else { $this->stateInfo = $this->getStateInfo("ERROR_UNKNOWN"); } return Response()->json($result) ->header('Access-Control-Allow-Headers', '*') ->header('Access-Control-Allow-Headers', 'X-Requested-With,X_Requested_With'); //->header("Content-Type", "text/html; charset=utf-8"); } /** * 上传错误检查 * @param $errCode * @return string */ private function getStateInfo($errCode) { try { return !$this->stateMap[$errCode] ? $this->stateMap["ERROR_UNKNOWN"] : $this->stateMap[$errCode]; } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } }