1

I'm using sfValidatorFile and sfWidgetInputFile to support user upload file, which allow file types: doc, docx, zip and rar. It has no problem with them but Symfony throws error with files *.rar : Invalid mime type (application/octet-stream).

Here my validator config:

$this->validatorSchema['contract_file'] = new sfValidatorFile(array(
      'required'   => true,
      'path'       => sfConfig::get('sf_upload_dir').'/contracts',
      'mime_types' => array(
        'application/msword',
        'application/zip',
        'application/x-rar-compressed',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
       ),
       'max_size' => $maxSize
    ));

I don't know why Symfony throws this errors while I have set mime_types for it? How to fix for upload *.rar?

UPDATE

When clear mime_types config, Symfony allow upload file *.rar but it rename to *.bin. Why?

Community
  • 1
  • 1
Davuz
  • 5,040
  • 13
  • 41
  • 61
  • I found the same problem here http://stackoverflow.com/questions/2104127/uploaded-docx-files-turning-into-zip?rq=1 But now I'm at home and not allow using my company computer. Somebody help me confirm that! Thank's a lot! – Davuz Jun 12 '14 at 15:31
  • According to this question http://stackoverflow.com/questions/6977544/rar-zip-files-mime-type you should also use the mime type `application/octet-stream` when dealing with archive. – Michal Trojanowski Jun 13 '14 at 06:41
  • @MichalTrojanowski I have tried `application/octet-stream` but SF still rename file to *.bin – Davuz Jun 13 '14 at 09:09

1 Answers1

0

If you have a look into the sfValidatedFile class there is a function there which sets the extension for application/octet-stream mime type to .bin. You can create your own class which extends the sfValidatedFile and overwrite the said file.

When configuring the sfValidatorFile you can pass the option validated_file_class with the name of your own class.

Something like this:

class myValidatedFile extends sfValidatedFile
{
    protected function getExtensionFromType($type, $default = '')
    {
        static $extensions = array(
            // ...
            'application/octet-stream' => '.rar',
            // ...  
        );

        return !$type ? $default : (isset($extensions[$type]) ? '.'.$extensions[$type] : $default);
    }
}

And in the form's configure function:

 $this->validatorSchema['contract_file'] = new sfValidatorFile(array(
     // ...
     'validated_file_class' => 'myValidatedFile',
));
Michal Trojanowski
  • 10,641
  • 2
  • 22
  • 41
  • Oh, but what if user uploads *.exe? We can't check, `octet-stream` is generic for many file types. – Davuz Jun 15 '14 at 08:04
  • 1
    I thought you only wanted the user to upload doc, docx, zip and rar files. You can fiddle with the `myValidatedFile` class to suit your need - e.g. for `application/octet-stream` files use the original extension. – Michal Trojanowski Jun 15 '14 at 12:49
  • yeah. I think the best is custom our validator. I will try at tomorow. Thanks a lot! – Davuz Jun 15 '14 at 14:33