Set Mime_type validation in Symfony

三世轮回 提交于 2019-12-09 13:58:46

问题


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.


回答1:


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'
)



回答2:


There is a way to constraint file selection from upload in a browser.
The drawback is it uses flash to select file, and limit support to users having flash player installed. The advantage is flash give you much more control over upload (parrallel upload, progression, errors, controle files extension allowed to select, etc.).

The component I use for this is swfupload

EDIT : this does not exempt from doing server-side control on uploaded files, but used in combination it offers a similar security leel and a much better user experience (errors can happens before a long-running upload start)



来源:https://stackoverflow.com/questions/688926/set-mime-type-validation-in-symfony

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!