"临时文件错误", "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 upload(Request $request) { try { $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"); break; case 'uploadimage': $upload_config = array( "pathFormat" => $config['imagePathFormat'], "maxSize" => $config['imageMaxSize'], "allowFiles" => $config['imageAllowFiles'] ); $field = $config['imageFieldName']; $bucket = config('alioss.buckets.picture'); break; case 'uploadscrawl': $upload_config = array( "pathFormat" => $config['scrawlPathFormat'], "maxSize" => $config['scrawlMaxSize'], "allowFiles" => $config['scrawlAllowFiles'], "oriName" => "scrawl.png" ); $field = $config['scrawlFieldName']; $bucket = config('alioss.buckets.file'); break; case 'uploadvideo': $upload_config = array( "pathFormat" => $config['videoPathFormat'], "maxSize" => $config['videoMaxSize'], "allowFiles" => $config['videoAllowFiles'] ); $field = $config['videoFieldName']; $bucket = config('alioss.buckets.picture'); break; case 'uploadfile': default: $upload_config = array( "pathFormat" => $config['filePathFormat'], "maxSize" => $config['fileMaxSize'], "allowFiles" => $config['fileAllowFiles'] ); $field = $config['fileFieldName']; $bucket = config('alioss.buckets.file'); break; } $file = $request->file($field); $oriName = $file->getClientOriginalName(); $fileSize = $file->getClientSize(); $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"); } $object = date('Y') . date('m') . "/" . date('d') . "/" . $this->GetRandStr(6) . time() . '.' . $fileType; $upload_result = $this->uploadToAliOSS($filePath, $object, $bucket); dd($upload_result); if ($upload_result['status']) { if (strpos($request->url(), 'admin.ufutx.net') !== false) { $result['url'] = $upload_result['path']; } else { $reg = '/(http):\/\/([^\/]+)/i'; preg_match($reg, $upload_result['path'], $res); $patterns[0] = '/' . $res[2] . '/'; $replacements[0] = 'image.fulllinkai.com'; $result['url'] = preg_replace($patterns, $replacements, $upload_result['path']); } $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"); } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } /** * 上传错误检查 * @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('服务器休息中,请稍后再试'); } } function uploadToAliOSS($file, $object, $bucket) { try { $accessKeyId = config('alioss.id');//涉及到隐私就不放出来了 $accessKeySecret = config('alioss.secret');//涉及到隐私就不放出来了 $endpoint = config('alioss.host');//节点 $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); $ossClient->setTimeout(3600 /* seconds */); $ossClient->setConnectTimeout(10 /* seconds */); // 先把本地的example.jpg上传到指定$bucket, 命名为$object dd($bucket); $ossClient->uploadFile($bucket, $object, $file); $signedUrl = $ossClient->signUrl($bucket, $object); $path = explode('?', $signedUrl)[0]; $obj['status'] = true; $obj['path'] = $path; } catch (OssException $e) { $obj['status'] = false; $obj['path'] = ""; print $e->getMessage(); } return $obj; } function GetRandStr($length) { try { //字符组合 $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $len = strlen($str) - 1; $randstr = ''; for ($i = 0; $i < $length; $i++) { $num = mt_rand(0, $len); $randstr .= $str[$num]; } return $randstr; } catch (\Exception $e) { $this->getError($e); return $this->failure('服务器休息中,请稍后再试'); } } function index(Request $request) { return $this->success('ok'); } }