3

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. Commented Nov 23, 2015 at 8:19
  • 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. Commented Nov 23, 2015 at 8:23
  • Could you post the code for the entire html form? Or at least show us how you build the form tags. Commented Nov 23, 2015 at 8:26

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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
]

1 Comment

If this would be the case, wouldn't the web server return an error?
0

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

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

2 Comments

Thanks for all responses
The actual part to implement is using {!! Form::open(['url'=>'admins','files'=>true]) !!} instead of {!! Form::open(['url'=>'admins','enctype'=>'multipart/form-data']) !!}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.