dongshen7561 2014-03-25 14:42
浏览 28
已采纳

ZF2文件上传:文件为空

I have a problem with uploading Images in a Zend Framework 2 Application and i think i have a configuration error somewhere in my code.

This is my current Code. I created the Form and Filter like the ZfcUser Form.

Form extends ProvidesEventsForm(ZfcBase)

$file = new Element\File('image');
$file->setLabelAttributes(array('class' => 'control-label col-sm-4'));
$file->setLabel('image');
$this->add($file);

Filter extends ProvidesEventsInputFilter(ZfcBase)

$this->add(
    array(
        'type' => 'Zend\InputFilter\FileInput',
        'name' => 'image',
        'required' => true,
        'validators' => array(
            array(
                'name' => 'File\UploadFile',
            ),
        ),
        'filters' => array(
            array(
                'name' => 'File\RenameUpload',
                'options' => array(
                    'target' => './public/img/uploads',
                    'randomize' => true,
                ),
            ),
        ),
    )
);

In the validation process the method FileInput::isValid() is called - but the Value is always null and i have no clue why it is.

The HTML-Form is set to multipart/form-data and the Server configuration is also no problem. The file i used for testing is only 80KB.

Anyone an idea, what is wrong?

  • 写回答

1条回答 默认 最新

  • dongzhun1857 2014-03-26 07:30
    关注

    Finally i found a solution for the Problem.

    Before the Post information added to the form, they have to merged with the file information:

    $files = $this->getRequest()->getFiles()->toArray();
    $post = array_merge_recursive(
        $this->getRequest()->getPost()->toArray(),
        $files
    );
    
    $form->setData($post);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘