上传的docx文件变成拉链 [英] Uploaded docx files turning into zip

查看:223
本文介绍了上传的docx文件变成拉链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的symfony 1.4,并想允许用户上载的Microsoft Word docx文件。使用sfWidgetFormInputFile部件和sfValidatorFile低于用户能够选择并成功地用一个简单的Web表单上传自己的docx文件。

I am currently using symfony 1.4 and would like to allow users to upload Microsoft Word docx files. Using the sfWidgetFormInputFile widget and sfValidatorFile below users are able to select and successfully upload their docx files using a simple web form.

$this->widgetSchema['file_name'] = new sfWidgetFormInputFile(array('label' => 'File'));

$this->validatorSchema['file_name'] = new sfValidatorFile(array(
  'required'   => true,
  'path'       => sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.sfConfig::get('app_dir_file_sharing').DIRECTORY_SEPARATOR,
  'mime_types' => array('application/msword',
                        'application/vnd.ms-word',
                        'application/msword',
                        'application/msword; charset=binary')
), array(
    'invalid'    => 'Invalid file.',
    'required'   => 'Select a file to upload.',
    'mime_types' => 'The file must be a supported type.'
));

的问题是,该文件被上传之后,扩展被改变为.zip和文件中包含的XML文件的文件树。我的理解是,这是因为Office 2007现在使用Open XML文件格式。有没有什么办法prevent此使用的symfony或PHP怎么回事?

The problem is that after the file is uploaded, the extension is changed to .zip and the file contains a file tree of xml files. My understanding is that this is because Office 2007 are now using Open xml file formats. Is there any way to prevent this from happening using symfony or PHP?

推荐答案

Symfony的1.3+有一个选项 mime_type_guessers sfValidatorFile 它允许你定义自己的MIME类型猜测器调用PHP或猜测者使用的版本。调用任何3内置的MIME类型的猜测中找到正确的文件类型DOCX,并保持了的docx文件扩展名。

Symfony 1.3+ has an option mime_type_guessers for sfValidatorFile which allows you to define your own mime type guesser PHP callable or use a build in guesser. Calling any of the 3 built-in mime type guessers finds the correct file type for docx and keeps the the docx file extension.

下面是一个使用guessFromFileinfo更新code:

Here is the updated code using guessFromFileinfo:

$this->validatorSchema['file_name'] = new sfValidatorFile(array(
'required'   => true,
'path'       => sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.sfConfig::get('app_dir_file_sharing').DIRECTORY_SEPARATOR,
'mime_type_guessers' => array('guessFromFileinfo'),
'mime_types' => array('application/msword',
                    'application/vnd.ms-word',
                    'application/msword',
                    'application/msword; charset=binary')
), array(
    'invalid'    => 'Invalid file.',
    'required'   => 'Select a file to upload.',
    'mime_types' => 'The file must be a supported type.'
));

这篇关于上传的docx文件变成拉链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆