如何在使用zend addvalidator验证该文件之前获取上载的文件名和大小

时间:2012-02-27 06:46:34

标签: php zend-framework file-upload

使用zend add验证器验证文件大小和扩展名时。是否可以跟踪上传文件的文件名和大小。即使验证失败,我们也可以获得文件的大小和名称。我无法跟踪验证失败时文件名,因为验证失败时文件没有进入临时目录。在存储到临时目录之前验证文件是否正确

这是我的代码段:

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

1 个答案:

答案 0 :(得分: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