Set mime_types not working for zip file in Symfony

陌路散爱 提交于 2019-12-13 03:25:30

问题


In my Symfony application, I want to set zip file as one of the mime type during the file upload, the code to do this is below:

    $this->validatorSchema ['Documents'] = new sfValidatorFile ( 
        array ('mime_types' => array(
        'application/zip', 
        'image/jpeg',
    'image/pjpeg',
    'image/png',
    'image/x-png',
   'image/gif',
        'application/x-zip',
        'application/octet-stream',
        'application/pdf') ), 
        array ('invalid' => 'Invalid file.',
         'required' => 'Select a file to upload.', 
         'mime_types' => 'The file must be of JPEG, PNG , GIF, pdf and zip format.' ) );

However, when I did the uploads, I found that all of pdf, png, gif etc can be uploaded. The only mime type that cannot be uploaded, are files that end with .zip.

What's going wrong here?

I asked a related, but different question here.

Note: I am using Symfony 1.2.7

Edit: I did some further testing with different browsers. Firefox 3 works because of 'application/octet-stream'', whereas IE works because of 'application/x-zip-compressed', Google Chrome doesn't work at all.


回答1:


After more testing, I believe that this is a bug in Symfony.




回答2:


Do an echo or error_log to see what $uploaded_file->getMime() [or whatever the right method call is] returns for your zip files.

If the string you get is one of the strings you pass to the array, there might be a bug with sfValidatorFile (I've never used it) and you might want to try using a yml validator.




回答3:


this is a problem with mime type detection. the sfValidatorFile can use user supplied function to determine mime type. so you can write your own function to detect zip files from uploaded file if mime type that is determined by validator doesn't do the job right.

link to documentation, look at the end of page that explains file uploads.




回答4:


There is a solution :

  1. create your "sfValidatorFileZip.class.php" in your "/lib/Validator/". This class extend from the commun "sfValidatorFile" and it contain the "configure" methode as listed below :

    class sfValidatorFileZip extends sfValidatorFile{
      protected function configure($options = array(), $messages = array()){
        if (!ini_get('file_uploads'))
        {
          throw new LogicException(sprintf('Unable to use a file validator as "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path')));
        }
    
        $this->addOption('max_size');
        $this->addOption('mime_types');
        $this->addOption('mime_type_guessers', array(
          array($this, 'guessFromFileinfo'),
          array($this, 'guessFromMimeContentType'),
          array($this, 'guessFromFileBinary'),
        ));
        $this->addOption('mime_categories', array(
          'web_images' => array(
            'image/jpeg',
            'image/pjpeg',
            'image/png',
            'image/x-png',
            'image/gif',
        ),
          'zip_file' => array(
                'application/zip'
        )
            ));
        $this->addOption('validated_file_class', 'sfValidatedFile');
        $this->addOption('path', null);
    
        $this->addMessage('max_size', 'File is too large (maximum is %max_size% bytes).');
        $this->addMessage('mime_types', 'Invalid mime type (%mime_type%).');
        $this->addMessage('partial', 'The uploaded file was only partially uploaded.');
        $this->addMessage('no_tmp_dir', 'Missing a temporary folder.');
        $this->addMessage('cant_write', 'Failed to write file to disk.');
        $this->addMessage('extension', 'File upload stopped by extension.');
      }
    }
    
  2. in your form, the widget will be an instance from your custom "sfValidatorFileZip" an you have to precise that 'mime_types' => 'zip_file'. exemple :

    $this->setValidator('filename', new sfValidatorFileZip(array(
                'required' => false,
                'mime_types' => 'zip_file',
                'path' => sfConfig::get('sf_upload_dir') . '/files/',), 
                array('required' => 'filename is required.', 'mime_types' => 'only ZIP is accepted')));
    


来源:https://stackoverflow.com/questions/852017/set-mime-types-not-working-for-zip-file-in-symfony

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