当使用zend添加验证器来验证文件大小和扩展名时。是否可以跟踪上传文件的文件名和大小。即使验证失败,我们也能得到文件的大小和名称吗。验证失败时我无法跟踪文件名,因为验证失败时文件不会进入临时目录。文件在存储到临时目录之前是否经过验证
这是我的代码片段:
$document_path_field = $this->CreateElement('file','document_path');
$document_path_field->setLabel('Document');
$document_path_field->setAttrib('class','button');
//$document_path_field->setDestination(SUPPORTING_DOCUMENT_DIRECTORY);
$document_path_field->addValidator('Count', false, 1);
$document_path_field->addPrefixPath('Course_Validate_File', 'Course/validate/File', 'validate');
$document_path_field->addValidator('Size', false, 1000000);
$document_path_field->addPrefixPath('Course_Validate_File', 'Course/validate/File', 'validate');
$document_path_field->addValidator('CheckExtension',false,'docx,doc,jpg,png,gif,pdf');
$document_path_field->clearDecorators();
if(isset($field_required_array['document_path']) && $field_required_array['document_path'] == "Yes")
{
$document_path_field->setRequired(true);
}
else
{
$document_path_field->setRequired(false);
}
$document_path_field->setDecorators($this->setFieldElementDecorators());
if(in_array('document_path',$field_names_array))
{
array_push($form_elements,$document_path_field);
}
$current_document_path = $this->CreateElement('hidden','current_document_path');
$current_document_path->setLabel('Current Document')
->clearDecorators()
->addDecorator($imageviewScript)
->setValue($this->_document_path);
array_push($form_elements,$current_document_path);
您发布的代码看起来像是用于窗体,而不是用于控制器,所以它对tbh没有真正的帮助。
在示例2中:http://framework.zend.com/manual/en/zend.file.transfer.introduction.html#zend.file.transfer.introduction.checking
在第二个if语句中,您可以简单地获得文件名:
if (!$upload->isValid($file)) {
print "Sorry but $file is not what we wanted";
continue;
} else echo 'File '.$info['name'].' is not allowed';
更多信息:使用zend框架1.7.4 上传文件