3
votes

I want to upload an image file using

 {!! Form::open(['url'=>'admins']) !!}
  {!! Form::input('file','photo',null,['class'=>'photo_input']) !!} 

Also my validation rules are

 public function rules()
{
    return [
       'username'=>'required|max:127|min:3|unique:users,username,'.$this->username,
       'email'=>'required|max:127|email|min:3|unique:users,email,'.$this->email,
       'password'=>'required|max:127|min:5|confirmed',
       'password_confirmation'=>'required|max:127|min:5|',
       'role'=>'required|max:127|min:5|in:programmer,admin,employee',
       'photo' => 'mimes:jpg,jpeg,bmp,png,gif'
    ];
}

But I get an error

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

Whereas the file extension which I choose is jpg, so what's wrong?

3
Where did you get this image file? It should have a mime type of jpeg, not only the file extension counts.Jerodev
It's an common image file and I'm sure that it's mime type is jpg, also I tried other images but the result was the same.Tohid Dadashnezhad
Could you post the code for the entire html form? Or at least show us how you build the form tags.Jerodev

3 Answers

1
votes

The reason behind is you need to define within your form tag the attribute enctype = "multipart/form-data". So while using Laravel 5.X Form Facade you need to pass the attribute files => true within your array of form open like as

{!! Form::open(['url'=>'admins','files' => true]) !!}
                              //^^^^^^^^^^^^^^^^ added

Source Docs

0
votes

This issue is also caused due to uploading large file size then the allowed in the php.ini

Please check

upload_max_filesize

and

post_max_size

in php.ini

You can also validate the Image size by extending the Validator

In Your Controller
Validator::extend('img_upload_size', function($attribute, $value, $parameters)
{
 $file = Request::file($attribute);
 $image_size = $file->getClientSize();
 if( (isset($parameters[0]) && $parameters[0] != 0) && $image_size > $parameters[0]) return false;
 return true;
});


In Validation Rules
 return [
 'username'=>'required|max:127|min:3|unique:users,username,'.$this->username,
    'email'=>'required|max:127|email|min:3|unique:users,email,'.$this->email,
    'password'=>'required|max:127|min:5|confirmed',
    'password_confirmation'=>'required|max:127|min:5|',
    'role'=>'required|max:127|min:5|in:programmer,admin,employee',
 'photo' => 'required|mimes:jpeg,bmp,png|img_upload_size:1000000',//!MB
]
0
votes

I fixed it, the reason was that I have not added enctype to the form.

{!! Form::open(['url'=>'admins','enctype'=>'multipart/form-data']) !!}