0

为了创建业务,我上传了 5 张图片和一个 .csv 文件。我使用了以下 zend 验证器

$upload = new Zend_File_Transfer();
        $upload->addValidator('Count', false, array('min' =>1, 'max' => 6))
               ->addValidator('Size', false, array('max' => '1Mb'))
               ->addValidator('ImageSize', false, array('minwidth' => 50,
                                                        'maxwidth' => 1000,
                                                        'minheight' => 50,
                                                        'maxheight' => 1000));

当我上传 CSV 时,我收到一条错误消息,提示未检测到 ImageSize。有什么方法可以跳过 .csv 文件的 ImageSize 验证器吗?

4

2 回答 2

1

在这里,示例验证第一个图像的高度为 435 像素,宽度为 175 像素,所有其他剩余图像的高度为 200 像素,宽度为 200 像素。

$targetPath = $this->registry->DOC_ROOT.'/public/uploads/images/campersite_user_photo/';

            if(!is_dir($targetPath))
            {
                mkdir($targetPath,'0777');
            }

            $adapter->setDestination($targetPath);

            $first = true;
            $filecheck = '';

            if(isset($asAdminVal['admin_role_id']) && ($asAdminVal['admin_role_id'] == '1' || $asAdminVal['admin_role_id'] == '2'))
            {
                $photoCount = Model_TblCampersiteUserPhotos::getCamperPhotoCount($this->view->snCampId);

                if($photoCount == 0)
                {
                    $j = 1;

                    foreach ($adapter->getFileInfo() as $fields => $info) 
                    {
                        if($info['name'] != '' && $first == true)
                        {
                            $filecheck = $fields;
                        }

                        if($filecheck != '' && $first == true)
                        {
                            $form->photo_path->addValidator('ImageSize', false,array('minwidth' => 435,'minheight' => 175,'messages' => array('fileImageSizeWidthTooSmall' => $this->translate->_('msg_camper_banner_image_file_too_width_height_less'),'fileImageSizeHeightTooSmall' => $this->translate->_('msg_camper_banner_image_file_too_width_height_less'))),$fields);
                            $first = false;
                        }
                        else
                        {
                            $form->photo_path->addValidator('ImageSize', false,array('minwidth' => 200,'minheight' => 200,'messages' => array('fileImageSizeWidthTooSmall' => $this->translate->_('msg_file_too_small'),'fileImageSizeHeightTooSmall' => $this->translate->_('msg_file_too_small'))),$fields);
                        }
                        $fileInfo[$j] = $info;
                        $j++;
                    }
                }
                else
                {
                    $j = 1;
                    foreach ($adapter->getFileInfo() as $fields => $info) 
                    {
                        $form->photo_path->addValidator('ImageSize', false,array('minwidth' => 200,'minheight' => 200,'messages' => array('fileImageSizeWidthTooSmall' => $this->translate->_('msg_file_too_small'),'fileImageSizeHeightTooSmall' => $this->translate->_('msg_file_too_small'))),$fields);
                        $fileInfo[$j] = $info;
                        $j++;
                    }   
                }
            }
            else
            {
                $j = 1;
                foreach ($adapter->getFileInfo() as $fields => $info) 
                {
                    $form->photo_path->addValidator('ImageSize', false,array('minwidth' => 200,'minheight' => 200,'messages' => array('fileImageSizeWidthTooSmall' => $this->translate->_('msg_file_too_small'),'fileImageSizeHeightTooSmall' => $this->translate->_('msg_file_too_small'))),$fields);
                    $fileInfo[$j] = $info;
                    $j++;                   
                }
            }

要验证表单,请编写以下代码。

if($form->isValid($formData))
{

}
于 2011-08-31T08:49:48.310 回答
0

我想出了怎么做!

$upload = new Zend_File_Transfer();
    $files = $upload->getFileInfo();
    foreach ($files as $fields => $contents)
    {
        if ($fields!= 'filename6')  -- Skipping the validation for csv file
        {
            $upload->addValidator('Count', false, array('min' =>1, 'max' => 6),$fields)
                   ->addValidator('Size', false, array('max' => '1Mb'),$fields)
                   ->addValidator('ImageSize', false, array('minwidth' => 50,
                                                            'maxwidth' => 1000,
                                                            'minheight' => 50,
                                                            'maxheight' => 1000),$fields);
        }
        elseif ($fields == 'filename6') -- To validate only csv file
        {
            $upload->addValidator('Extension', false, 'csv', $fields)
                   ->addValidator('Size', false, array('max' => '1Mb'),$fields);
        }
    }          
于 2011-06-14T20:34:13.633 回答