@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).
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">
@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"
What's the file name that you are trying to upload?
the filename of image i used for testing is sampleimage.png
Have you checked the mime type of the image file?
@opheliadesign here is what i am using for my request rules
public function rules()
{
return [
'picture' => 'required | mimes:jpeg,jpg,png | max:1000',
];
}
yes i already checked it, also i tried other files like .jpeg and .jpg
Show your controller.
here is my what i have in my controller for the image upload
$user->picture = Input::file('picture');
$user->save();
Hmm just curious, are you processing the file upload at all? Like saving it to disk somewhere?
Show your code! We can do a lot of guessing, but that will take ages and get you nowhere.
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?
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.
@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();
@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();
}
@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
@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">
@PLB-RR 👍👍👍
@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
@Bishwajit if the answer here didn't solve your problem, then please make a new thread describing your issue.
@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
@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?
@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.
@PLB-RR @opheliadesign thanks a lot i appreciate you help :)
What if i want to upload a video?
Please or to participate in this conversation.