如何使用Zend \ InputFilter \ Factory上传多个文件?

时间:2014-01-24 11:00:37

标签: php zend-framework

我正在使用Zend Framework 2.2.5。 我遇到了某些问题。

准备以下控制器。

public function indexAction()
{
    $form = new \Zend\Form\Form();

    $form->add(array(
        'type' => 'Zend\Form\Element\File',
        'name' => 'file',
        'attributes' => array(
            'multiple' => TRUE,
        )
    ));

    /** @+ */
    $factory = new \Zend\InputFilter\Factory();
    $file = $factory->createInput(array(
        'name' => 'file',
        'filters' => array(
            array(
                'name' => 'Zend\Filter\File\RenameUpload',
                'options' => array(
                    'target' => './data/tmpuploads/',
                    'overwrite' => TRUE,
                    'use_upload_name' => TRUE,
                ),
            ),
        ),
    ));
    /** @- */
    /** @+ */
//    $file = new \Zend\InputFilter\FileInput('file');
//    $file->getFilterChain()->attach(
//        new \Zend\Filter\File\RenameUpload(array(
//            'target' => './data/tmpuploads/',
//            'overwrite' => TRUE,
//             'use_upload_name' => TRUE,
//        )
//    ));
    /** @- */
    $inputFilter = new \Zend\InputFilter\InputFilter();
    $inputFilter->add($file);
    $form->setInputFilter($inputFilter);

    if ($this->getRequest()->isPost()) {
        $form->setData(array_merge_recursive(
            $this->getRequest()->getPost()->toArray(),
            $this->getRequest()->getFiles()->toArray()
        ));
        if ($form->isValid()) {
            \Zend\Debug\Debug::dump($form->getData());
        }
    }

    $form->setAttribute('method', 'post')
            ->setAttribute('action', $this->url()->fromRoute('examples'))
            ->prepare();

    return new \Zend\View\Model\ViewModel(array('form' => $form));
}

出现以下警告。

Warning: Illegal offset type in isset or empty in /mnt/shared/zf2/vendor/zendframework/zendframework/library/Zend/Filter/File/RenameUpload.php on line 175
Warning: basename() expects parameter 1 to be string, array given in /mnt/shared/zf2/vendor/zendframework/zendframework/library/Zend/Filter/File/RenameUpload.php on line 262
Warning: file_exists() expects parameter 1 to be a valid path, array given in /mnt/shared/zf2/vendor/zendframework/zendframework/library/Zend/Filter/File/RenameUpload.php on line 180

问题是要解决如果你切换评论,但我想使用Zend \ InputFilter \ Factory。

我该如何解决这个问题?为什么会出现此警告?

1 个答案:

答案 0 :(得分:0)

我用以下方式解决了这个问题。

$factory = new \Zend\InputFilter\Factory();
$file = $factory->createInput(array(
    'name' => 'file',
    'type' => 'Zend\InputFilter\FileInput',
    'filters' => array(
        array(
            'name' => 'Zend\Filter\File\RenameUpload',
            'options' => array(
                'target' => './data/tmpuploads/',
                'overwrite' => TRUE,
                'use_upload_name' => TRUE,
            ),
        ),
    ),
));