File/Image validation in slim framework Request validation

908 views Asked by At

I wanted to validate the uploaded image with following parameters like size should not be greater than 100KB, file can be of types [jpg,png,gif]. Please help me to write validation rules for this in Slim framework. I'm using Respect validator.

$files = $request->getUploadedFiles();
$validation = $this->validator->validate($request, [
        'name' => v::notEmpty(),
        'description' => v::notEmpty(),
        'logo' => v::size('100KB')->mimetype('image/png,image/png,image/gif')->validate($files['logo']->getClientFilename()),
    ]);
    if ($validation->failed()) {
        $errors = $validation->errors;
        $print_r($errors);
    }

This is how I'm using validation rules. Everything works except logo validation.

1

There are 1 answers

9
Devaux On

You can do something to validate your file size like this

v being the validator you are using

v::size('1KB')->validate($filename); // Must have at least 1KB size

You can do the following to validate the mine types

v::mimetype('image/png')->validate('image.png'); // true

Using the Respect validatio, this might be useful http://respect.github.io/Validation/docs/validators.html