2012-02-27 37 views
1

使用zend时添加验证程序以验证文件大小和扩展名。是否可以跟踪文件名和上传文件的大小。可以获取大小和文件名,即使验证失败。当验证失败时我无法跟踪文件名,因为文件在验证失败时未进入临时目录。是在存储在临时目录中之前验证的文件如何在使用zend addvalidator验证该文件之前获取上传的文件名和大小

这是我的代码片段:

 $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); 

回答

0

你已经发布的代码看起来它是一种形式,而不是控制器,它不是真的帮助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'; 

更多信息:File Upload using zend framework 1.7.4

相关问题