我一直在研究他们的 FIleInput 类的 Laminas文档,但我还没有找到关于这些过滤器和验证器实际作用的正确解释。
我正在建立一个社区网站并计划让用户上传文件,并且我想对这些上传的文件应用安全检查,我对此进行了很多研究,并且我计划进行我在 a 中找到的图像安全检查StackOverflow 中有很多线程(这里和这里),但我想对非图像上传的文件进行一些其他检查/验证。
那么 Laminas\InputFilter\FileInput 真的可以做到吗?或者它到底做了什么?
我一直在研究他们的 FIleInput 类的 Laminas文档,但我还没有找到关于这些过滤器和验证器实际作用的正确解释。
我正在建立一个社区网站并计划让用户上传文件,并且我想对这些上传的文件应用安全检查,我对此进行了很多研究,并且我计划进行我在 a 中找到的图像安全检查StackOverflow 中有很多线程(这里和这里),但我想对非图像上传的文件进行一些其他检查/验证。
那么 Laminas\InputFilter\FileInput 真的可以做到吗?或者它到底做了什么?
您可以使用以下内容确保用户发送正确的文件图像:
public function addInputFilter()
{
$inputFilter = new InputFilter\InputFilter();
// File Input
$fileInput = new InputFilter\FileInput('image-file');
$fileInput->setRequired(true);
// Define validators and filters as if only one file was being uploaded.
// All files will be run through the same validators and filters
// automatically.
$fileInput->getValidatorChain()
->attachByName('filesize', ['max' => 204800])
->attachByName('filemimetype', ['mimeType' => 'image/png,image/x-png'])
->attachByName('fileimagesize', ['maxWidth' => 100, 'maxHeight' => 100]);
// All files will be renamed, e.g.:
// ./data/tmpuploads/avatar_4b3403665fea6.png,
// ./data/tmpuploads/avatar_5c45147660fb7.png
$fileInput->getFilterChain()->attachByName(
'filerenameupload',
[
'target' => './data/tmpuploads/avatar.png',
'randomize' => true,
]
);
$inputFilter->add($fileInput);
$this->setInputFilter($inputFilter);
}
输入过滤器列表在这里:
文档:https ://docs.laminas.dev/laminas-validator/validators/file/intro/