401, 'msg' => '未授权', 'data' => []]); } $file = $this->request->file('file'); if (!$file) { return json(['code' => 400, 'msg' => '请选择文件', 'data' => []]); } $max = 10 * 1024 * 1024; if ($file->getSize() > $max) { return json(['code' => 400, 'msg' => '文件不能超过 10MB', 'data' => []]); } $ext = strtolower((string) $file->extension()); if (!in_array($ext, ['jpg', 'jpeg', 'png', 'webp', 'gif'], true)) { return json(['code' => 400, 'msg' => '仅支持 jpg/png/webp/gif', 'data' => []]); } $subdir = date('Ymd'); $root = public_path() . 'uploads' . DIRECTORY_SEPARATOR . $subdir; if (!is_dir($root) && !mkdir($root, 0755, true) && !is_dir($root)) { return json(['code' => 500, 'msg' => '无法创建上传目录', 'data' => []]); } $saveName = bin2hex(random_bytes(8)) . '.' . $ext; $file->move($root, $saveName); $url = '/uploads/' . $subdir . '/' . $saveName; return json(['code' => 0, 'msg' => 'ok', 'data' => ['url' => $url]]); } }