Zend Framework 2-使用输入过滤器验证器上载时获取新文件名 [英] Zend Framework 2 - get the new filename when uploading using a validator for inputfilter
问题描述
我是ZF2的新手,我正在尝试上传文件并使用验证器中重命名"文件适配器过滤器的随机"选项.
I am new to ZF2, and I am trying to upload a file and use the "randomize" option of the Rename file adapter filter inside the validator.
在模型中,我有这个:
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/'
)
)
)
));
.....
在图像验证器中,我有这个:
And in the Image validator I have this:
$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值返回给Model,以便可以将其写入数据库?还是您有其他建议可以做到这一点?
How can I get the $newFileName value back to the Model so I can write it to the database? Or do you have other suggestions to get this done?
谢谢!
推荐答案
对于文件输入,有一个特殊的FileInput
类.您可以在此处找到文档.对于文件上传(还包括图像),建议在输入过滤器中使用此特殊输入类.
For file input there is a special FileInput
class. You can find documentation here. For file uploads (also images) it is recommended to use this special input class inside your input-filter.
如果您在执行它时遇到问题,请发表评论.
If you have problems implementing it just leave a comment.
这篇关于Zend Framework 2-使用输入过滤器验证器上载时获取新文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!