我想确保在文件上传期间,只允许使用 jpeg、png 和 gif 格式的文件。所以截图下面的“文件类型:”必须显示jpeg、png和gif:
http://lh5.ggpht.com/_SDci0Pf3tzU/ScynOZt_0qI/AAAAAAAAEo0/gMr_zxYofV4/s400/styleerror.png
我在 Symfony 中为我的验证器做了以下操作:
$this->setValidator ( 'upload your filehere',
new sfValidatorFile ( array (
'required'=>true,
'mime_types' => array ('image/jpeg, image/png, image/gif' )
) ,
array(
'mime_types'=> 'only jpeg'
)
)
);
但是当我点击上传按钮时,文件没有被相应地过滤;我做错什么了?
我也试试
$this->setValidator ( 'upload your filehere',
new sfValidatorFile ( array (
'required'=>true,
'mime_types' => 'image/jpeg, image/png, image/gif'
) ,
array(
'mime_types'=> 'only jpeg'
)
)
);
但它也不起作用。
我在我的表单类中尝试了以下方法,但不幸的是它不起作用:
<?php
class UploadCaseForm extends sfForm {
public function configure() {
$this->setWidgets ( array ('Documents' => new sfWidgetFormInputFile ( ) ) );
$this->widgetSchema->setNameFormat ( 'UploadCase[%s]' );
$this->validatorSchema ['Documents'] = new sfValidatorFile (
array ('mime_types' => 'web_images' ),
array ('invalid' => 'Invalid file.',
'required' => 'Select a file to upload.',
'mime_types' => 'The file must be of JPEG, PNG or GIF format.' ) );
}
}
?>
这是操作代码:
public function executeIndex(sfWebRequest $request) {
$this->form = new UploadCaseForm ( );
if ($this->getRequest ()->getMethod () == sfRequest::POST) {
var_dump($request->getParameter('UploadCase'));
}
}
编辑:就服务器端验证而言,接受的答案就是答案。如果有人想要客户端验证,即在操作到达服务器之前过滤可以上传的文件类型,那么由于浏览器的限制,可能没有可靠的方法来做到这一点。