为什么ZF2 PRG插件返回[fileUploadFileErrorFileNotFound] =>找不到档案?

时间:2016-05-09 01:45:01

标签: php zend-framework2 post-redirect-get

我一直在将此示例改编为我自己的表单:http://framework.zend.com/manual/2.1/en/modules/zend.form.file-upload.html

当我使用表单上的Choose File按钮添加文件,然后提交表单时,我明白了:

Array
(
    [fileUploadFileErrorFileNotFound] => File was not found
)

上面打印出的变量内容是这样的(提交表格后):

$fileErrors = $form->get('character-ref')->getMessages();

可能是什么问题?在这一点上,我有点不清楚。我查看了RenameUpload过滤器和一些网站,但此时缺乏线索。

我的代码相关:

class CommissionInputFilter implements InputFilterAwareInterface
{
    function getInputFilter()
    {
        $inputFilter = new InputFilter();

        //this is the filter that is supposed to make 
        //PRG Plugin file upload possible
        $renameFilter = new RenameUpload(array(
            'target' => "./data/uploads/",
            'randomize' => true
        ));

        // File Input - I am not sure if this is the way to do it
        //some examples do not use FileInput
        $fileInput = new FileInput('character-ref');
        $fileInput->setRequired(false);
        $fileInput->getFilterChain()->attach($renameFilter);
        $inputFilter->add($fileInput);
        return $inputFilter;
    }
}


//code inside controller 

public function addAction()
{
    $form = new CommissionForm();
    $tempFile = null;

    $prg = $this->fileprg($form);

    if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
        return $prg;
    } elseif (is_array($prg)) {

        //**************************************************
        //this line below is where I set my input filter for the form
        //the filter that contains PRG Rename pluging supposedly
        //One interesting note is if I *Remove* the line below
        //then the PRG Plugin keeps the file name as it is supposed to
        //**************************************************

        $form->setInputFilter((new CommissionInputFilter())->getInputFilter());
        if ($form->isValid()) {

            $output = new CommissionOutput();

            $commission = $output->getCommission($form->getData());
            $id = $this->repository->saveCommission($commission);

            $view = new ViewModel();
            $view->setTemplate('commission/commission/thank_you');
            $view->setVariables($prg);
            return $view;
        } else {

            //********************************************************
            //My Properly-filled out form ends up here with file errors
            //********************************************************

            $fileErrors = $form->get('character-ref')->getMessages();
            if (empty($fileErrors)) {
                $tempFile = $form->get('character-ref')->getValue();
            }
        }
    }
    return array(
        'form' => $form,
        'characterRefFile' => $tempFile
    );
}

1 个答案:

答案 0 :(得分:0)

哦,我想发布它有帮助。

因为我找到了原因。显然,

  1. 表单需要正确设置表单过滤器[完成]
  2. PRG需要RenameUpload过滤器设置[完成那个]
  3. 最重要的是,必须在创建表单之后和PRG之前设置表单过滤器[未完成]
  4. $inputFilter = (new CommissionInputFilter())->getInputFilter();
    
    $form = new CommissionForm();
    $form->setInputFilter($inputFilter);
    $prg = $this->fileprg($form);
    

    必须在创建表单之后和PRG插件之前设置过滤。