【问题标题】:Set Mime_type validation in Symfony在 Symfony 中设置 Mime_type 验证
【发布时间】:2009-03-27 08:30:37
【问题描述】:

我想确保在文件上传期间,只允许使用 jpeg、png 和 gif 格式的文件。所以截图下面的“File of type:”必须显示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'));


    }

}

编辑:就服务器端验证而言,接受的答案答案。如果有人想要客户端验证,即在操作到达服务器之前过滤可以上传的文件类型,那么可能是there is no reliable way to do it, because of browser's constraint

【问题讨论】:

    标签: symfony1


    【解决方案1】:

    您似乎错误地使用了 API。 sfForm::setValidator() 的签名是here

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

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

    “web_images”类别包括以下 MIME 类型:

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

    如果您想明确定义要接受的 MIME 类型,请将“web_images”替换为如下类型的数组:

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

    【讨论】:

    • 谢谢,但我的 mime 类型不应该只包含图像;我还需要包含 zip 文件或 word 文档文件
    • 好的,从您的问题来看,您似乎只想要图像。我已经编辑了我的答案,并解释了如何设置自定义 MIME 类型。
    • 不幸的是,它似乎不起作用;我已经更新了问题
    • 在我想要客户端验证的意义上它似乎不起作用——但如果你想要服务器端验证它确实有效。并且客户端验证规范从未在大多数浏览器中正确实现。
    【解决方案2】:

    有一种方法可以限制在浏览器中上传文件的选择。
    缺点是它使用 flash 来选择文件,并且限制了对安装了 flash 播放器的用户的支持。优点是 Flash 让您可以更好地控制上传(并行上传、进度、错误、允许选择的控制文件扩展名等)。

    我为此使用的组件是swfupload

    编辑:这并不能免除对上传文件进行服务器端控制,但结合使用它可以提供类似的安全级别和更好的用户体验(在长时间运行的上传开始之前可能会发生错误)

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 2021-06-14
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 2022-01-14
      • 1970-01-01
      • 2015-09-21
      相关资源
      最近更新 更多