0

我一直在研究他们的 FIleInput 类的 Laminas文档,但我还没有找到关于这些过滤器和验证器实际作用的正确解释。

我正在建立一个社区网站并计划让用户上传文件,并且我想对这些上传的文件应用安全检查,我对此进行了很多研究,并且我计划进行我在 a 中找到的图像安全检查StackOverflow 中有很多线程(这里这里),但我想对非图像上传的文件进行一些其他检查/验证。

那么 Laminas\InputFilter\FileInput 真的可以做到吗?或者它到底做了什么?

4

1 回答 1

0

您可以使用以下内容确保用户发送正确的文件图像:

    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);
    }

输入过滤器列表在这里:

  • 数数
  • CRC32
  • 排除扩展
  • 排除MimeType
  • 存在
  • 扩大
  • 文件大小
  • 哈希
  • 图片尺寸
  • 已压缩
  • 是图像
  • MD5
  • 哑剧类型
  • 不存在
  • 沙1
  • 尺寸
  • 上传
  • 上传文件
  • 字数

文档:https ://docs.laminas.dev/laminas-validator/validators/file/intro/

于 2021-08-19T12:59:39.383 回答