如何在 zend Framework2 中使用“filerenameupload”过滤器?

问题描述 投票:0回答:2

我在表单类中添加文件元素:

        $this->add(array(
        'type' => 'Zend\Form\Element\File',
        'name' => 'logo_file',
        'options' => array(
            'label' => 'Select your logo image file:',
        ),
    ));

然后在模型中添加过滤器来过滤表单数据。我使用“filerenameupload”过滤器上传选定的文件:

        $inputFilter = new InputFilter();
        $inputFilter->add($factory->createInput(array(
            'name'     => 'logo_file',
            'required' => false,
            'filters'  => array(
                array('name' => 'filerenameupload',
                    'options'=>array(
                        //'target'    => "./data/logo.png",
                        'randomize' => true,
                    )
                ),
            ),
        )));

在控制器中我通常调用 setInputFilter、setData 和 isValid。其他元素过滤良好,但“logo_file”不会保存在“./data/logo.png”中。 事实上“Zend\Filter\File\RenameUpload”类中的“filter”函数不会被执行。

我使用这个链接: zf2 文件上传教程

有人正在尝试解决这个问题吗?

php forms filter file-upload zend-framework2
2个回答
2
投票

您是否尝试过使用过滤器的全名?

array(
    'name' => 'Zend\Filter\File\RenameUpload'
)

您还需要确保在验证时将文件数组和 POST 数据添加到表单中,例如:

$postArr = $request->getPost()->toArray();
$fileArr = $this->params()->fromFiles('logo_file');
$formData = array_merge(
     $postArr, // $_POST
     array('logo_file' => $fileArr['name']) // $_FILE...
);
$importForm->setData($formData);

0
投票

尝试:

表单.php:

public function addElements(){
    $this->add(array(
        'name' => 'image',
        'attributes' => array(
            'type' => 'file',
        ),
        'options' => array(
        ),
    ));

表单验证器.php

public function getInputFilter()
{
    if (!$this->inputFilter) {

        $inputFilter = new InputFilter();
        $factory = new InputFactory();

        $inputFilter->add($factory->createInput(array(
                    "name" => "image",
                    "required" => true,
                    "filters" => array(
                        array("name" => "StripTags"),
                        array("name" => "StringTrim"),
                        array(
                            "name" => "Zend\Filter\File\RenameUpload",
                            "options" => array(
                                "target" => '/home/limonazzo/UPLOADDIR<----------',
                                "randomize" => true,
                                "use_upload_name" => true,
                                "use_upload_extension" => true
                            )
                        )
                    ),
                    "validators" => array(
                        array(
                            "name" => "Zend\Validator\File\IsImage",
                            "break_chain_on_failure" => true,
                            "options" => array(
                            ),
                        ),
                        array(
                            "name" => "Zend\Validator\File\Extension",
                            "break_chain_on_failure" => true,
                            "options" => array(
                                "extension" => "jpg,jpeg,png",
                            ),
                        ),
                        array(
                            "name" => "Zend\Validator\File\Size",
                            "break_chain_on_failure" => true,
                            "options" => array(
                                "min" => "1kB",
                                "max" => "1024kB",
                            ),
                        ),
                        array(
                            "name" => "Zend\Validator\File\ImageSize",
                            "break_chain_on_failure" => true,
                            "options" => array(
                                "minWidth" => 10,
                                "minHeight" => 10,
                                "maxWidth" => 250,
                                "maxHeight" => 350,
                            ),
                        ),
                    ),
        )));

在control.php中

$form = new Form();
$formValidator = new FormValidator();
$form->setInputFilter($formValidator->getInputFilter(''));
if ($form->isValid()) {
    $data = $form->getData(\Zend\Form\FormInterface::VALUES_AS_ARRAY);
    $imgName = $data["image"]["tmp_name"];
© www.soinside.com 2019 - 2024. All rights reserved.