在 Symfony 中设置 Mime_type 验证 [英] Set Mime_type validation in Symfony

查看:26
本文介绍了在 Symfony 中设置 Mime_type 验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保在文件上传期间,只允许使用 jpeg、png 和 gif 格式的文件.因此,屏幕截图下方的文件类型:"必须显示 jpeg、png 和 gif:

http://lh5.ggpht.com/_SDci0Pf3tzU/ScynOZt_0AEqI/AAAAAAA/gMr_zxYofV4/s400/styleerror.png

我在 Symfony 中为我的验证器做了以下操作:

$this->setValidator ( '在此处上传您的文件',新 sfValidatorFile ( 数组 ('必需'=>真,'mime_types' =>数组('图像/jpeg,图像/png,图像/gif')) ,大批('mime_types'=>'只有 jpeg')));

但是当我点击上传按钮时,文件没有被相应地过滤;我做错了什么?

我也试试

$this->setValidator ( '在此处上传您的文件',新 sfValidatorFile ( 数组 ('必需'=>真,'mime_types' =>'图像/jpeg、图像/png、图像/gif') ,大批('mime_types'=>'只有 jpeg')));

但它也不起作用.

我在我的表单类中尝试了以下内容,但不幸的是它不起作用:

setWidgets ( array ('Documents' => new sfWidgetFormInputFile ( ) ) );$this->widgetSchema->setNameFormat ('UploadCase[%s]');$this->validatorSchema ['Documents'] = new sfValidatorFile (数组('mime_types' => 'web_images' ),数组('无效' => '无效文件.','必需' =>'选择要上传的文件.','mime_types' =>'文件必须是 JPEG、PNG 或 GIF 格式.) );}}?>

这是操作代码:

 公共函数 executeIndex(sfWebRequest $request) {$this->form = new UploadCaseForm ( );if ($this->getRequest()->getMethod() == sfRequest::POST) {var_dump($request->getParameter('UploadCase'));}}

就服务器端验证而言,接受的答案答案.如果有人想要客户端验证,即在操作到达服务器之前过滤可以上传的文件类型,那么也许 由于浏览器的限制,没有可靠的方法可以做到这一点.

解决方案

您似乎错误地使用了 API.sfForm::setValidator() 的签名是这里.>

但是,这里有一个简单的方法来设置一个只允许上传网络图片的文件验证器:

$this->validatorSchema['my_file'] = new sfValidatorFile(array('mime_types' =>'网络图像'), 大批('无效' =>'无效的文件.','必需' =>'选择要上传的文件.','mime_types' =>'文件必须是 JPEG、PNG 或 GIF 格式.));

web_images"类别包括以下 MIME 类型:

图像/jpeg图像/pjpeg图像/png图像/x-png图像/gif

如果要明确定义要接受的 MIME 类型,请将web_images"替换为如下所示的类型数组:

'mime_types' =>大批('图像/jpeg','图像/pjpeg','图像/png','图像/x-png','图像/gif')

I want to make sure that during file upload time, only the file of the format jpeg, png and gif are allowed. So the "File of type:" below in the screenshot must show jpeg, png and gif:

http://lh5.ggpht.com/_SDci0Pf3tzU/ScynOZt_0qI/AAAAAAAAEo0/gMr_zxYofV4/s400/styleerror.png

I did the following for my validator in 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'
      )
      ) 
      );

but when I click on the upload button, the files are not filtered accordingly; what did I do wrong?

I also try

$this->setValidator ( 'upload your filehere',
     new sfValidatorFile ( array (
     'required'=>true,
     'mime_types' => 'image/jpeg, image/png, image/gif' 
      ) ,
      array(

      'mime_types'=> 'only jpeg'
      )
      ) 
      );

But it doesn't work, too.

I tried the following in my form class, but unfortunately it doesn't work as well:

<?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.' ) );

    }
}
?>

Here's the action code:

    public function executeIndex(sfWebRequest $request) {
        $this->form = new UploadCaseForm ( );

if ($this->getRequest ()->getMethod () == sfRequest::POST) {
        var_dump($request->getParameter('UploadCase'));


    }

}

Edit: The accepted answer is the answer, as far as the server side validation goes. If anyone wants a client side validation, i.e., filtering the type of file that can be uploaded even before the operation hits server, then maybe there is no reliable way to do it, because of browser's constraint.

解决方案

You seem to be using the API incorrectly. The signature of sfForm::setValidator() is here.

However, here's a simple way to set a file validator that allows only web images to be uploaded:

$this->validatorSchema['my_file'] = 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.'
));

The 'web_images' category includes the following MIME types:

image/jpeg
image/pjpeg
image/png
image/x-png
image/gif

If you want to explicitly define the MIME types to accept, replace 'web_images' with an array of types like this:

'mime_types' => array(
    'image/jpeg',
    'image/pjpeg',
    'image/png',
    'image/x-png',
    'image/gif'
)

这篇关于在 Symfony 中设置 Mime_type 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆