zend-framework2zend-form2

ZF2.4 File input RenameUpload


I have got a fieldset implementing InputFilterProviderInterface, so it has the function getInputFilterSpecification. The __construct() function is adding a file element ('logo') like so:

    $this->add(array(
        'name' => 'logo',
        'type' => 'file',
        'attributes' => array(
            'required' => false,
            'class' => 'form-control',
            'id' => 'logo',
        ),
        'options' => array(
            'label' => 'Company Logo: ',
        ),
    ));

In my getInputFilterSpecification function how do I add the RenameUpload input filter?

I have tried several variations of the following without success:

public function getInputFilterSpecification()
{
    return array(
        
        array(
            'name' => 'logo',
            'filterchain' => array(
                'name' => 'filerenameupload',
                'options' => array(
                    'target' => '/public/images/' . time(),
                ),
            ),
            'required' => false,
        ),
        //... other filters
    }
}

How do I add the FileRenameUpload filter (Zend\Filter\File\RenameUpload)?

[edit]

I have changed the array to:

        array(
            'name' => 'logo',
            'filters' => array(
                array(
                    'name' => 'filerenameupload',
                    'options' => array(
                        'target' => '/public/images/' . time(),
                    ),
                ),
            ),
            'required' => false,
        ),

Which "appears" to be working, however I am now getting this message -

File 'C:\xampp\tmp\phpDA68.tmp' could not be renamed. An error occurred while processing the file.

What errors could have occurred? How do I fix it?


Solution

  • This issue is being caused by specifying an invalid target. Prefixing it with getcwd() makes the path relative to the application root.

    array(
        'name' => 'logo',
        'filters' => array(
            array(
                'name' => 'filerenameupload',
                'options' => array(
                    'target' => getcwd() . '/public/images/' . time(),
                ),
            ),
        ),
        'required' => false,
    ),