0

在我的 Symfony 应用程序中,我想在文件上传期间将 zip 文件设置为 mime 类型之一,执行此操作的代码如下:

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

但是,当我进行上传时,我发现所有 , 等都pdf可以png上传gif。唯一不能上传的 mime 类型是以 . 结尾的文件.zip

这里出了什么问题?

我在这里问了一个相关但不同的问题。

注意:我使用的是 Symfony 1.2.7

编辑:我用不同的浏览器做了一些进一步的测试。Firefox 3 工作是因为 'application/octet-stream'',而 IE 工作是因为 'application/x-zip-compressed',谷歌浏览器根本不工作。

4

4 回答 4

1

经过更多测试,我认为这是 Symfony 中的一个错误

于 2009-05-13T08:56:53.780 回答
0

执行 echo 或 error_log 以查看 $uploaded_file->getMime() [或任何正确的方法调用] 为您的 zip 文件返回的内容。

如果您获得的字符串是您传递给数组的字符串之一,则 sfValidatorFile 可能存在错误(我从未使用过它),您可能想尝试使用 yml 验证器。

于 2009-05-12T12:56:09.700 回答
0

有一个解决方案:

  1. 在“/lib/Validator/”中创建“sfValidatorFileZip.class.php”。此类从commun“sfValidatorFile”扩展而来,它包含“configure”方法,如下所示:

    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. 在您的表单中,该小部件将是您自定义的“sfValidatorFileZip”中的一个实例,您必须准确说明'mime_types' => 'zip_file'. 例子:

    $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')));
    
于 2012-05-10T12:11:48.797 回答
0

这是 mime 类型检测的问题。sfValidatorFile 可以使用用户提供的函数来确定 mime 类型。因此,如果验证器确定的 mime 类型不能正确完成工作,您可以编写自己的函数来检测上传文件中的 zip 文件。

链接到文档,查看解释文件上传的页面末尾。

于 2009-05-13T08:24:35.940 回答