Php 如何在form-zend framework 2中创建数组元素

Php 如何在form-zend framework 2中创建数组元素,php,zend-framework2,Php,Zend Framework2,我要创建以下元素: <input type="file" name="file[]"> 及 但它不工作。用于文件上载 使用这个类很重要,因为它还做其他重要的事情,比如。还有一些为您重命名上载的方法 考虑到$this是您的InputFilter实例,代码可能如下所示: $this->add(array( 'name' => 'file', 'required' => true, 'allow_empty' => false, '

我要创建以下元素:

<input type="file" name="file[]">

但它不工作。

用于文件上载

使用这个类很重要,因为它还做其他重要的事情,比如。还有一些为您重命名上载的方法

考虑到
$this
是您的
InputFilter
实例,代码可能如下所示:

$this->add(array(
    'name' => 'file',
    'required' => true,
    'allow_empty' => false,
    'filters' => array(
        array(
            'name' => 'File\RenameUpload',
            'options' => array(
                'target' => 'upload',
                'randomize' => true,
                'overwrite' => true
            )
        )
    ),
    'validators' => array(
        array(
            'name' => 'FileSize',
            'options' => array(
                'max' => 10 * 1024 * 1024 // 10MB
            )
        )
    ),
    // IMPORTANT: this will make sure you get the `FileInput` class
    'type' => 'Zend\InputFilter\FileInput'
);
要将文件元素附加到表单,请执行以下操作:

// File Input
$file = new Element\File('file');
$file->setLabel('My file upload')
     ->setAttribute('id', 'file');
$this->add($file);
查看有关文件上载的更多信息。 或者检查如何制作文件上传的上传表单

使用这个类很重要,因为它还做其他重要的事情,比如。还有一些为您重命名上载的方法

考虑到
$this
是您的
InputFilter
实例,代码可能如下所示:

$this->add(array(
    'name' => 'file',
    'required' => true,
    'allow_empty' => false,
    'filters' => array(
        array(
            'name' => 'File\RenameUpload',
            'options' => array(
                'target' => 'upload',
                'randomize' => true,
                'overwrite' => true
            )
        )
    ),
    'validators' => array(
        array(
            'name' => 'FileSize',
            'options' => array(
                'max' => 10 * 1024 * 1024 // 10MB
            )
        )
    ),
    // IMPORTANT: this will make sure you get the `FileInput` class
    'type' => 'Zend\InputFilter\FileInput'
);
要将文件元素附加到表单,请执行以下操作:

// File Input
$file = new Element\File('file');
$file->setLabel('My file upload')
     ->setAttribute('id', 'file');
$this->add($file);
查看有关文件上载的更多信息。
或者查看如何制作上传表单

谢谢您的回复。我尝试了您的代码,但出现了错误:执行过程中发生了错误;请稍后再试。其他信息:Zend\Form\Exception\InvalidElementException文件:/var/www/html/actifiti/admin/vendor/ZF2/library/Zend/Form/FormElementManager.php:121消息:Zend\InputFilter\FileInput类型的插件无效;必须实现Zend\Form\ElementInterface@ErFaiyazAlam
$this
在本例中是一个
InputFilter
类。尝试添加到输入筛选器实例而不是表单。@ErFaiyazAlam我编辑了我的答案并添加了更多链接和示例。有很多关于文件上传的文档。谢谢你的回复。我尝试了您的代码,但出现了错误:执行过程中发生了错误;请稍后再试。其他信息:Zend\Form\Exception\InvalidElementException文件:/var/www/html/actifiti/admin/vendor/ZF2/library/Zend/Form/FormElementManager.php:121消息:Zend\InputFilter\FileInput类型的插件无效;必须实现Zend\Form\ElementInterface@ErFaiyazAlam
$this
在本例中是一个
InputFilter
类。尝试添加到输入筛选器实例而不是表单。@ErFaiyazAlam我编辑了我的答案并添加了更多链接和示例。有很多关于文件上传的文档。