简体   繁体   English

苗条框架中的文件/图像验证请求验证

[英]File/Image validation in slim framework Request validation

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]. 我想使用以下参数来验证上传的图片,例如大小不应该大于100KB,文件可以是[jpg,png,gif]类型。 Please help me to write validation rules for this in Slim framework. 请帮助我在Slim框架中为此编写验证规则。 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. 除徽标验证外,一切正常。

You can do something to validate your file size like this 您可以执行以下操作来验证文件大小:

v being the validator you are using v是您正在使用的验证器

v::size('1KB')->validate($filename); v ::大小( '1KB') - >验证($文件名); // Must have at least 1KB size //大小必须至少为1KB

You can do the following to validate the mine types 您可以执行以下操作来验证地雷类型

v::mimetype('image/png')->validate('image.png'); v :: mime类型( '图像/ PNG') - >验证( 'image.png'); // true //正确

Using the Respect validatio, this might be useful http://respect.github.io/Validation/docs/validators.html 使用“尊重”验证,这可能很有用http://respect.github.io/Validation/docs/validators.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM