简体   繁体   English

如何使用Respect \\ Validation库验证文件上载?

[英]How can i validate file upload using Respect\Validation library?

I use Slim 3 Framework on my project, and i have a form with 3 inputs : 'file', 'name', 'firstname' . 我在我的项目中使用了Slim 3 Framework ,我有一个包含3个输入的表单: 'file', 'name', 'firstname'

Assuming that $request has the data inputs of my form, to get the uploaded file, i use this code 假设$request具有我的表单的数据输入,为了获取上传的文件,我使用此代码

$files = $request->getUploadedFiles();
    if (empty($files['file'])) {
        throw new Exception('Invalid image');
    }
$newfile = $files['file'];

Then, to validate my input forms, i use the Respect\\Validation Library 然后,为了验证我的输入表单,我使用Respect \\ Validation Library

$validation = $this->validator->validate($request, [
        'file' => v::file()->mimetype('application/pdf'),
        'name' => v::stringType()->notEmpty()->length(2, 50)->alpha(),
        'firstname' => v::stringType()->notEmpty()->length(2, 50)->alpha()
]);


if($validation->failed() {
     //...
}

The fact is the file validation always fails : 事实是文件验证总是失败:

var_dump($request->getParam('file')); //return NULL
var_dump($newfile); //return the following

$newfile content $newfile内容

object(Slim\Http\UploadedFile)#59 (8) { 
    ["file"]=> string(14) "/tmp/php409Uje" 
    ["name":protected]=> string(47) "Myfile.pdf" 
    ["type":protected]=> string(15) "application/pdf" 
    ["size":protected]=> int(1404476) ["error":protected]=> int(0) 
    ["sapi":protected]=> bool(true) ["stream":protected]=> NULL 
    ["moved":protected]=> bool(false) 
}

So now I'm stuck. 所以现在我被卡住了。 I really can't figure out how can i tell the validator to validate $newfile MIMETYPE for the 'file' input. 我真的无法弄清楚如何告诉验证器验证$newfile MIMETYPE的'file'输入。

You can validate the mimetype using the file attribute of UploadedFile class . 您可以使用UploadedFilefile属性验证mimetype。

For example: 例如:

$uploadedFiles = $request->getUploadedFiles();
$uploadedFile = $uploadedFiles['file'];

$validationResult = v::file()->mimetype('application/pdf')->validate($uploadedFile->file);
if($validationResult){
    echo "Validation ok";
} else {
    echo "Validation error";
}

Files and other parameters are handled in different ways so you can't access files using the classic getParam($name) method. 文件和其他参数以不同方式处理,因此您无法使用经典的getParam($name)方法访问文件。 The bad news about it is that you can't use a single validation step, but you have to use one for all the POST parameters and another one for the files. 关于它的坏消息是你不能使用单个验证步骤,但你必须使用一个用于所有POST参数,另一个用于文件。 This means in your specific case that you have to do something like this: 这意味着在您的特定情况下,您必须执行以下操作:

$validation = $this->validator->validate($request, [
        'file' => v::file()->mimetype('application/pdf'),
        'name' => v::stringType()->notEmpty()->length(2, 50)->alpha(),
        'firstname' => v::stringType()->notEmpty()->length(2, 50)->alpha()
]);

$uploadedFiles = $request->getUploadedFiles();

if (empty($uploadedFiles['file'])) {
    throw new Exception('Invalid image');
}

$uploadedFile = $uploadedFiles['file'];

$fileValidationResult = v::file()->mimetype('application/pdf')->validate($uploadedFile->file);

if($validation->failed() || !$fileValidationResult) {
     //...
}

Maybe too late but you can try 也许为时已晚,你可以试试

v::objectType()->attribute('file', v::oneOf(
    v::mimetype('application/pdf'),
    v::mimetype('image/png')
))->validate($uploadedFile);

Where $uploadedFile is an instance of \\Slim\\Http\\UploadedFile 其中$ uploadedFile是\\ Slim \\ Http \\ UploadedFile的一个实例

You can as many validators as you want in "oneOf" 您可以在“oneOf”中使用尽可能多的验证器

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

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