0

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.

Community
  • 1
  • 1
Graviton
  • 81,782
  • 146
  • 424
  • 602

2 Answers2

3

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'
)
Ree
  • 6,061
  • 11
  • 48
  • 50
  • Thanks, but my mime type shouldn't just include image only; I also need to include zip files, or word document files – Graviton Mar 27 '09 at 09:14
  • OK, from your question it would seem like you wanted images only. I have edited my answer with an explanation on how to set custom MIME types. – Ree Mar 27 '09 at 09:32
  • Unfortunately it doesn't seem to work; I have updated the question – Graviton Mar 27 '09 at 10:09
  • It doesn't seem to work in the sense that I want client side validation-- but it does work if you want server side validation. And client side validation specs are never properly implemented in the most browsers. – Graviton Mar 27 '09 at 16:15
0

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)

Benoit
  • 3,569
  • 2
  • 22
  • 20