我一直在为他们的FIleInput类研究Laminas documentation,但我还没有找到一个合适的解释来解释这些过滤器和验证器到底是做什么的。
我正在建立一个社区网站,并计划让用户上传文件,我想对这些上传的文件应用安全检查,我对此进行了很多研究,我计划在StackOverflow (here和here)的许多线程中发现图像安全检查,但我想对非图像上传的文件做一些其他检查/验证。
那么Laminas\InputFilter\FileInput真的能做到这一点吗?或者它到底是做什么的?
发布于 2021-08-19 12:59:39
您可以确保用户使用以下内容发送正确的文件镜像:
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/
https://stackoverflow.com/questions/65889019
复制相似问题