【问题标题】:issue with image validator rules in laravel 4laravel 4 中的图像验证器规则问题
【发布时间】:2015-02-21 11:14:03
【问题描述】:

我在 laravel 4 中为文件(图像)定义了这个验证规则。

'image' => 'image|mimes:jpeg,jpg,bmp,png,gif'

这是我的 html 代码:

{{ Form::label('Image of the product') }} {{ Form::file('image') }}

现在,即使我选择 png、jpg 图像,我在上传图像时总是会出现 2 个错误

The image must be an image.
The image must be a file of type: jpeg, jpg, bmp, png, gif. 

这是包含图片上传的更新操作:

public function update($id)
  {
    $product = Product::find($id);
    $validator = Validator::make($data = Input::all(), Product::$rules);
    if($validator->fails())
      {
        return Redirect::back()->withErrors($validator)->withInput();
      }
      $old_img = "";
    if(isset($data['image']) and !empty($data['image']))
      {

        $image = Input::file('image');
        $filename = time().'-'.$image->getClientOriginalName();
        Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products/'.$filename);

      $data['image'] = 'img/products/'.$filename;
      $old_img = $product->image;
      }
    $product->update($data);
    File::delete('public/'.$old_img);
    return Redirect::route('admin.products');
  }

【问题讨论】:

  • 您能否提供 HTML 以防万一您在那里做错了什么。发生错误的具体步骤是什么?
  • {{ Form::label('产品图片') }} {{ Form::file('image') }}
  • 你试过只使用哑剧,像这样:'image' => 'mimes:jpeg,jpg,bmp,png,gif'
  • 你能告诉我们完整的控制器动作的代码吗?
  • 您是否在表单的配置数组中设置了 'files' => 'true'?

标签: html image laravel


【解决方案1】:

首先将此添加到您的表单标签中
enctype="multipart/form-data"
第二个验证只是'file'=>'required|mimes:jpeg,png,jpg,gif,svg'

验证 mime 并删除图像

【讨论】:

    【解决方案2】:

    通过添加确保您的表单是多部分的

    'files' => 'true'
    

    到配置数组。

    【讨论】:

      猜你喜欢
      • 2013-06-21
      • 1970-01-01
      • 2016-11-22
      • 2015-06-28
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      相关资源
      最近更新 更多