Laravel图片上传验证异常:Postman测试File::image()不通过
Laravel图片上传验证无效问题排查
我在Laravel项目中验证上传的图片,使用Postman测试时明明已上传文件,但File::image()验证仍判定输入无效。
我的验证规则如下:
'product_name' => 'required|string|unique:' . Product::class, 'product_stock_amount' => 'required|integer|numeric|gte:0', 'product_price' => 'required|integer|numeric|gte:0', 'product_price_currency' => 'required|string|'. Rule::in(config('lappee.accepted_currency')), 'product_description' => 'nullable|string', 'product_images' => [ 'nullable', File::image()->max(12 * 1024)->dimensions(Rule::dimensions()->maxWidth(config('lappee.allowed_image_size.width'))->maxHeight(config('lappee.allowed_image_size.height'))) ],
Postman配置截图:
可能的错误点:
- 字段名不匹配:从截图可见,Postman中上传文件的字段名是
product_image(单数),但验证规则里的字段是product_images(复数),这会导致验证规则无法匹配到上传的文件,直接判定无效。 - 单/多文件上传规则错误:如果是单文件上传,需将验证规则的字段名改为
product_image,与Postman中的字段名保持一致;如果是多文件上传,需要调整规则为数组验证(字段名保留product_images),同时Postman中字段名改为product_images[]:'product_images' => ['nullable', 'array'], 'product_images.*' => [ File::image()->max(12 * 1024)->dimensions(Rule::dimensions()->maxWidth(config('lappee.allowed_image_size.width'))->maxHeight(config('lappee.allowed_image_size.height'))) ], - 文件有效性问题:确认上传的文件是未损坏的标准图片格式(如JPG、PNG、GIF等),且文件大小、尺寸符合
config('lappee')中设置的限制。
内容的提问来源于stack exchange,提问作者Jheems
相关产品推荐
相关产品推荐

