简体   繁体   中英

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?

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

You must have created dir before use renameFile.

Example: mkdir( $rootPath . '/public/images/' . time() );

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