JackD's avatar

image validation

how to validation image based on file type and maximum file size?

i tried this

'image' => 'image:jpg,png|max:5000',

but i got a validation error even i upload a 2mb .png image

0 likes
24 replies
JackD's avatar

it says "The image must be an image"

JackD's avatar

but when i tried to make it

'image' => 'required'

it accepts the image, why it is like that?

deadlockgB's avatar

Ah now I see. You use the image rule incorrect. So this should work:

'image' => 'image|mimes:jpg,png|max:5000',
JackD's avatar

now im getting two this validation error, i already tried both jpg and png image to upload

The image must be an image.
The image must be a file of type: jpg, png.
deadlockgB's avatar

This means you are not uploading a .png image to your server. is the file in the

<input type="file" name="image" />

really a .png?

Is the 'image' rule applied to an 'image' field?

JackD's avatar

yes i tested already a jpg and png file but i got the same validation error message

JackD's avatar

here is my actual input tag for the file upload

<input type="file" name="image[]">
SachinAgarwal's avatar

@ci it should be like this

<input type="file" name="image">

it is not a array, so u should not make name as array.

1 like
JackD's avatar

@SachinAgarwal but i need it to be in an array because i have a button to append more input fields for image when clicked

JackD's avatar

@deadlockgB @SachinAgarwal

<form enctype="multipart/form-data" accept-charset="UTF-8" action="/route-to-post"" method="POST">
<form enctype="multipart/form-data" accept-charset="UTF-8" action="/route-to-post" method="POST">
    <input type="file" name="image[]" />
    <input type="file" name="image[]" /> //this will be added when a add more image button was clicked
</form>
Route::post('route-to-post', function()
{
    $files = \Input::file('files');
    $output = "";
    foreach ($files as $file) {
        $output .= $file->getClientOriginalName();
    }
    return $output;
});
SachinAgarwal's avatar

@Ci the prob is, when it is an array, you need to specify that its an array. Not sure weather it will work, but give it a try

'image' => 'array|image|mimes:jpg,png|max:5000',
deadlockgB's avatar

Also if you name the input fields "image[]" you will get them by accessing (the name attribute in the HTML is the accessor for the \input::files() array)

$images = \Input::file('image');

So the first thing you can validate if if $images (so all the images you uploaded in the 'image[]' input fields) is of type array:

$filesArArray = \Validator::make(
    ['images' => $images], // this is your \Input::file('image'); array from above
    ['images' => 'array'] // this is the rule for the array
);

After that you can loop through your images and see if the separat images are to your specification:

foreach($images as $image)
{
     $imagesAreImages = \Validator::make(
         ['image' => $image], // this is the single image instance
         ['image' => 'image|mimes:jpg,png|max:5000'] // this is the rule for a single image
     );
}

I hope you get the idea now

usman's avatar

@Ci here you go:

  //getting the image array.
  $file = app('request')->file('image');

  //define the basic rule that image should be array.
  $valid = app('validator')->make(compact('file'),['file'=>'required|array']);

  //for each array inside the array define a rule that : image cannot exceed 5000kb and it should be either png or a jpeg.
  $valid->each('file',['mimes:png,jpeg|max:5000']);

  //check your validation as usual here.
  $valid->passes(); //whatever.

Usman.

3 likes
JackD's avatar

@usman great but im getting this error "Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)" is it a bug?

deadlockgB's avatar

check your (correct) php.ini file. search for the line ;extension=php_fileinfo.dll and delete the ; at the front. Restart your webserver after that or if you use php-fpm restart the fpm deamon

JackD's avatar

@deadlockgB yes it is working now but the validation only works on the file format the file size is not being validated.. i tried to change the max from max:5000 to max:1000 and upload a 2MB jpg and it accept it, why it does not check the file size for validation?

JackD's avatar

i noticed that it is also not being required when i tried to submit empty field for the file even though there is a required in my code

deadlockgB's avatar

Allright I did some testing. I used a 24kb file named test.jpg and a 5kb file named test.png.

  • rule set 'image|mimes:jpeg,png|max:20': the validator passes the test.png but fails the test.jpg (because of the file size)
  • rule set 'image|mimes:jpeg,png|max:25': both files pass
  • rule set 'image|mimes:jpg,png|max:25': the test.jpg fails (notice the missing e in jpeg)

So in my end the validation works best when the mimes are set to mimes:jpg,jpeg,png and the size is set (as stated in the docs) as kilobytes. Not that the value you pass to max is divided by 1024 so you should propably use multiples of that for megabytes (5MB = 5120).

JackD's avatar

@deadlockgB yes it works on me now after i restart my xampp and browser

have i done it right in my code? though it is working but i find my code weird because of the two validator that i need to implement haha

       $files =\Input::file('image');
        $filesAreArray = \Validator::make(
            ['image'=>$files],
            ['image'=>'array']
        );

        foreach($files as $file)
        {
            $imagesAreImages = \Validator::make(
                ['image' => $file],
                ['mage' => 'required|image|mimes:jpg,png|max:2000']
            );
        }


      $validator = $this->registrar->validate_profile($request->all());

         if($validator->fails()) {
             
             $this->throwValidationException($request, $validator);
         }

        if($imagesAreImages->fails())
        {
            $this->throwValidationException($request, $imagesAreImages);
        }
deadlockgB's avatar

you could make it a lot mor readable with @usmans method from the first page (the $valif->each() function)

Please or to participate in this conversation.