You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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表单数据配置

可能的错误点:

  • 字段名不匹配:从截图可见,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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 12:15:39