ZF2.4 文件输入 RenameUpload

ZF2.4 File input RenameUpload

我有一个实现 InputFilterProviderInterface 的字段集,因此它具有函数 getInputFilterSpecification__construct() 函数正在添加一个文件元素 ('logo'),如下所示:

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

如何在我的 getInputFilterSpecification 函数中添加 RenameUpload 输入过滤器?

我已经尝试了以下几种变体,但均未成功:

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

如何添加 FileRenameUpload 过滤器 (Zend\Filter\File\RenameUpload)?

[编辑]

我已将数组更改为:

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

哪个“似乎”在工作,但是我现在收到这条消息 -

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

可能发生了什么错误?我该如何解决?

此问题是由指定无效 target 引起的。在它前面加上 getcwd() 使路径相对于应用程序根目录。

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

您必须在使用 renameFile 之前创建目录。

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