0

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.

Saurabh Sharma
  • 463
  • 2
  • 7
  • 20

1 Answers1

0

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

Devaux
  • 43
  • 1
  • 6
  • Thanks for the answer. I already went through this but it doesn't helped me because I have an array of supported of mime types. – Saurabh Sharma Nov 16 '18 at 12:31
  • @SaurabhSharma Have you tried something like this? `v::mimetype('image/png,image/png,image/gif')->validate('image.png')` – Devaux Nov 16 '18 at 12:34
  • Remember that the ->validate('image.png'); should be your file you are validating – Devaux Nov 16 '18 at 12:40
  • thanks for reply I have updated my question summery with my code. Please recheck, Still it's not working for me. – Saurabh Sharma Nov 19 '18 at 04:59
  • Just to clarify is it this line that is not working `'logo' => v::size('100KB')->mimetype('image/png,image/png,image/gif')->validate($files['logo']->getClientFilename()),` – Devaux Nov 19 '18 at 05:40
  • You can also remove your comma at the end of this line `'logo' => v::size('100KB')->mimetype('image/png,image/png,image/gif')->validate($files['logo']->getClientFilename()),` – Devaux Nov 19 '18 at 05:43
  • Only this logo rule doesn't work. If I remove this logo validation my validation works properly. If I use this something like **'logo' => v::size('100KB')->mimetype('image/png,image/png,image/gif')** and upload the image this every time give size and mime typ validation error even I'm uploading correct image. Probably the is with $request while uploading files. – Saurabh Sharma Nov 19 '18 at 15:01
  • is the image under 100KB? That is quite small for a image – Devaux Nov 20 '18 at 11:24
  • Yes my image is under 100KB – Saurabh Sharma Nov 22 '18 at 12:09