hasFile('image')) { return Rs::error('请选择要上传的图片'); } $file = $request->file('image'); // 验证文件类型 $allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']; if (!in_array($file->getMimeType(), $allowedTypes)) { return Rs::error('只允许上传JPG/PNG/GIF/WEBP格式的图片'); } // 验证文件大小(5MB) $maxSize = 5 * 1024 * 1024; if ($file->getSize() > $maxSize) { return Rs::error('图片大小不能超过5MB'); } // 生成唯一文件名 $filename = 'img_' . date('YmdHis') . '_' . uniqid() . '.' . $file->getClientOriginalExtension(); $path = $file->storeAs('images', $filename, 'public'); if ($path) { // 返回相对路径和完整URL $relativePath = 'storage/' . $path; $fullUrl = asset('storage/' . $path); return Rs::success([ 'url' => $relativePath, 'full_url' => $fullUrl ]); } else { return Rs::error('图片上传失败'); } } }