1
votes

I have defined this validating rule for a file(image) in laravel 4 .

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

And this is my html code:

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

now , I always get 2 errors while uploading an image even If I chose a png, jpg image

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

this is the update action that includes image upload :

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');
  }
2
Can you provide the HTML just in case you're doing something wrong there. What exact steps make the errors occur? - haakym
{{ Form::label('Image of the product') }} {{ Form::file('image') }} - arakibi
Have you tried it just using mimes, like so: 'image' => 'mimes:jpeg,jpg,bmp,png,gif' - haakym
Can you show us the code of the full controller action? - lukasgeiter
Have you set 'files' => 'true' in the config array for your form? - Joe

2 Answers

1
votes

Ensure that your form is a multipart one by adding

'files' => 'true'

to the config array.

0
votes

first add this to your form tag
enctype="multipart/form-data"
second in your validation just 'file'=>'required|mimes:jpeg,png,jpg,gif,svg'

validate mimes and remove image