0

I want to create the following element :

<input type="file" name="file[]">

I have tried the following code in myproject/module/Member/src/Member/Form/EditForm.php :

$this->add(array(
            'name' => 'file',
            'type'  => 'file',
            'attributes' => array(                
                'class' => 'form-control col-md-7 col-xs-12',                
                'id' => 'file',                
            ),'options' => array(
                'multiple'=>TRUE
            ),
        ));

and

$this->add(array(
            'name' => 'file[]',
            'type'  => 'file',
            'attributes' => array(                
                'class' => 'form-control col-md-7 col-xs-12',                
                'id' => 'file',                
            ),'options' => array(
                'multiple'=>TRUE
            ),
        ));

but it is not working.

1 Answer 1

4

For file upload Zend Framework 2 has a special FileInput class.

It is important to use this class because it also does other important things like validation before filtering. There are also special filters like the File\RenameUpload that renames the upload for you.

Considering that $this is your InputFilter instance the code could look like this:

$this->add(array(
    'name' => 'file',
    'required' => true,
    'allow_empty' => false,
    'filters' => array(
        array(
            'name' => 'File\RenameUpload',
            'options' => array(
                'target' => 'upload',
                'randomize' => true,
                'overwrite' => true
            )
        )
    ),
    'validators' => array(
        array(
            'name' => 'FileSize',
            'options' => array(
                'max' => 10 * 1024 * 1024 // 10MB
            )
        )
    ),
    // IMPORTANT: this will make sure you get the `FileInput` class
    'type' => 'Zend\InputFilter\FileInput'
);

To attach file element to a form:

// File Input
$file = new Element\File('file');
$file->setLabel('My file upload')
     ->setAttribute('id', 'file');
$this->add($file);

Check the documentation for more information on file upload. Or check the documentation here on how to make an upload form

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your reply. I tried your code but got error : An error occurred An error occurred during execution; please try again later. Additional information: Zend\Form\Exception\InvalidElementException File: /var/www/html/actifiti/admin/vendor/ZF2/library/Zend/Form/FormElementManager.php:121 Message: Plugin of type Zend\InputFilter\FileInput is invalid; must implement Zend\Form\ElementInterface
@ErFaiyazAlam $this is an InputFilter class in this case. Try to add to your input filter instance instead of your form.
@ErFaiyazAlam I edited my answer and added more links and examples. There is a lot of documentation on file upload.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.