简体   繁体   中英

ZF2 File Upload: File is empty

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM