orj's avatar
Level 3

Image validation The profile picture must be an image.

I have this problem with validating the image even though i am uploading a valid .png or .jpg file, i am using the latest version of laravel

I cant figure it out what is wrong in my code

regards ciby

Validation
'picture' => 'image|mimes:jpg,png',

View
<input type="file" class="form-control" name="picture" id="picture">
0 likes
26 replies
opheliadesign's avatar

@ciby personally I'd use one or the other. image which will accept jpeg, png, bmp, gif, or svg. With mimes you can just specify the types of images you will accept - and I'd add jpeg to the list, as I believe it's specific to how the file extension is entered (some are jpg, others jpeg).

orj's avatar
Level 3

@opheliadesign i tried already to change my validation to this

'picture' => 'required | mimes:jpeg,jpg,png | max:1000',

i tried again to upload a valid image file but i still get error message "The picture must be a file of type: jpeg, jpg, png"

2 likes
orj's avatar
Level 3

the filename of image i used for testing is sampleimage.png

PLB-RR's avatar

Have you checked the mime type of the image file?

orj's avatar
Level 3

@opheliadesign here is what i am using for my request rules

public function rules()
    {
        return [
            'picture' => 'required | mimes:jpeg,jpg,png | max:1000',

        ];
    }
orj's avatar
Level 3

yes i already checked it, also i tried other files like .jpeg and .jpg

orj's avatar
Level 3

here is my what i have in my controller for the image upload

$user->picture = Input::file('picture');
$user->save();
opheliadesign's avatar

Hmm just curious, are you processing the file upload at all? Like saving it to disk somewhere?

PLB-RR's avatar

Show your code! We can do a lot of guessing, but that will take ages and get you nowhere.

1 like
orj's avatar
Level 3

im not into file saving yet, im into file validation and saving its filename for now. i tried to add validation for the image upload and it is working, i dont know what is the problem when i add rule validation for my image it does not consider it as valid even it is a valid image file.

is it a bug?

opheliadesign's avatar

Tried to add validation for the image upload and it's working

.. huh? So is the image upload going somewhere else? Can you post your controller and the complete Form Request? We really need more code.

orj's avatar
Level 3

@PLB-RR @opheliadesign here is my code that i am trying now, i just change some part of it so i can show you my code

VIEW

<form class="form-horizontal" method="post">
                @foreach($errors->all() as $error)
                    <p class="alert alert-danger">{{ $error }}</p>
                @endforeach
                @if(session('status'))
                    <div class="alert alert-success">
                        {{ session('status') }}
                    </div>
                @endif

                {!! csrf_field() !!}
                <fieldset>
                    <div class="form-group">
                  
                            <input type="file" class="form-control" name="picture" id="picture">

                    </div>

                    <div class="form-group">
              
                            <button class="btn btn-default" type="reset">Cancel</button>
                            <button class="btn btn-primary" type="submit">Save changes</button>
                       
                    </div>
                </fieldset>
            </form>
CONTROLLER
public function update($user, Request $request) {
$rules = array(
            'picture' => 'required | mimes:jpeg,jpg,png | max:1000',
        );

        $validator = Validator::make($request->all(), $rules);

        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        }

 $user = User::where('id', Auth::user()->id)->firstOrFail();
 $user->save();

return Redirect::back();
opheliadesign's avatar

@ciby you started off with a Form Request to handle your validation - WhateverYourRequestNameIs.php. Were you injecting that in place of Request $request in your controller?

For example,

use App\Http\Requests\WhateverYourRequestNameIs;
public function update(WhateverYourRequestNameIs $request, $user) {
 $user = User::where('id', Auth::user()->id)->firstOrFail();
 $user->save();

return Redirect::back();
}
orj's avatar
Level 3

@opheliadesign no the image file it not going to my server folder yet, im into validation right now before proceeding to saving it to my server folder, im curious now what makes the file invalid even it is a valid image file based on what i set for validation

PLB-RR's avatar
PLB-RR
Best Answer
Level 3

@ciby add this to your form. Maybe this will fix it.

enctype="multipart/form-data"
<form class="form-horizontal" method="post" enctype="multipart/form-data">
31 likes
Bishwajit's avatar

@PLB-RR i was geeting the same error , but have used enctype and all , every thing is working properly just when i am trying to upload .jpg file it is giving me validation error file must be an image

Sinnbeck's avatar

@Bishwajit if the answer here didn't solve your problem, then please make a new thread describing your issue.

orj's avatar
Level 3

@opheliadesign no there is no request file right now for the validation rules, previously i tried to use the request file to validate my files but i get the same error message, that's why i used the old way using the validator

opheliadesign's avatar

AHHHH good catch, @PLB-RR! I've made that mistake a ton of times. That should solve the problem, @ciby.

Also, quick tip, if you want to get the ID of the user that made a form request you can just do $request->user()->id. In your controller you have $user as a parameter but then override it.

1 like
orj's avatar
Level 3

@PLB-RR What a mistake i made, thanks bro! it is working now! thanks also @opheliadesign :)

my concern now after figuring it out, the "max:1000 " in my validation rule for the image is not included in the error message, instead it just give an error message "The picture must be a file of type: jpeg, jpg, png ."

i wonder why it doesnt give me instead the error message regarding its files size?

PLB-RR's avatar

@ciby Your welcome :) I have had the same problem a number of times.

For the filesize: try manually setting a message and see if that works.

1 like

Please or to participate in this conversation.