Zend Framework 2 - 使用输入过滤器的验证器上传时获取新文件名

Zend Framework 2 - get the new filename when uploading using a validator for inputfilter

我是 ZF2 的新手,我正在尝试上传文件并使用验证器中重命名文件适配器过滤器的 "randomize" 选项。

在模型中我有这个:

public function getInputFilter()
    {

        $inputFilter = new InputFilter();
        $inputFilter->add(array(
            'name' => 'picture',
            'required' => call_user_func(function() use ($prize_type_id){
                if(isset($this->picture_old) && !empty($this->picture_old)){
                    return false;
                }
                if(in_array($prize_type_id, array(1,2,3,5))){
                    return true;
                }else{
                    return false;
                }
            }),
            'validators' => array(
                array(
                    'name' => '\Pm\Validators\File\Image',
                    'options' => array(
                            'minSize' => '1',
                            'maxSize' => '1024',
                            'newFileName' => null,
                            'uploadPath' => './public/uploads/images/'
                    )
                )
            )
        ));
.....

在图像验证器中我有这个:

$renameOptions['randomize'] = true;
$renameOptions['target'] = $uploadPath.$newFileName;
$this->filters[] = new Rename($renameOptions); 
$this->fileAdapter->setFilters($this->filters);
$this->fileAdapter->setValidators($this->validators); 
if ($this->fileAdapter->isValid()) { 
    $this->fileAdapter->receive();
    return $newFileName;
....

如何将 $newFileName 值返回给模型,以便将其写入数据库?或者您有其他建议来完成这项工作吗?

谢谢!

对于文件输入有一个特殊的 FileInput class。您可以找到文档 here。对于文件上传(还有图像),建议在您的输入过滤器中使用这个特殊输入 class。

如果您在实施时遇到问题,请发表评论。