【问题标题】:Can not upload doc or docx file in codeigniter无法在 codeigniter 中上传 doc 或 docx 文件
【发布时间】:2016-07-10 14:53:26
【问题描述】:

我正在使用 codeigniter 框架。我想上传 doc 或 docx 文件。所以我写了这样的代码:

$config['upload_path'] = './uploads/resume/';
$config['allowed_types'] = 'docx|doc|DOC|DOCX';

$this->upload->initialize($config);

if ( ! $this->upload->do_upload('resume'))
{
    print_r($error = array('error' => $this->upload->display_errors()));
}
else
{
    $upload = $this->upload->data();
    print_r($upload);exit;
}

在这里,它显示和错误,如不允许您尝试上传的文件类型。并且文件未上传。

我也修改了 mimes.php 文件并添加了

'doc'    =>    array('application/msword', 'application/vnd.ms-office'),
'docx'    =>    array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip', 'application/msword', 'application/x-zip'),

当我打印 $this->upload->data() 时,它将 file_type 显示为 application/vnd.oasis.opendocument.text

但是,它不起作用。那么我怎样才能上传 doc 或 docx 文件呢?

【问题讨论】:

  • 你的 CI 版本是多少?
  • @Abdulla 它的 2.2.6
  • 你加载库了吗??
  • @Abdulla 是的,我有加载库。

标签: php codeigniter


【解决方案1】:

在这里,我在 mimes.php 中创建了 doc 数组,如下所示:

'doc'    =>    array('application/msword', 'application/vnd.ms-office','application/vnd.oasis.opendocument.text')

及其工作原理。

【讨论】:

    【解决方案2】:

    我在允许的 mime 类型数组中没有看到“application/vnd.oasis.opendocument.text”。

    【讨论】:

    • 我应该在doc数组中写application/vnd.oasis.opendocument.text吗?
    • 是的,你应该这样做。如果您要上传的文件是 .doc,请将其添加到“doc”数组中。如果是 .docx 将其添加到该数组。您可能会发现更多的 mime 类型,这取决于创建文件的应用程序。因此,请务必在上传失败时记录该值,以便将来添加它们。
    【解决方案3】:

    不要忘记max_size 并允许输入txt

    $config['allowed_types']        = 'txt|doc|docx';
    $config['max_size']             = 10000;
    
    $this->load->library('upload', $config);
    

    如果仍然失败也加载 OpenOffice 扩展

    $config['allowed_types'] = 'application/vnd.oasis.opendocument.spreadsheet | application/vnd.oasis.opendocument.text | application/vnd.oasis.opendocument.presentation | 
    application/vnd.openxmlformats-officedocument.wordprocessingml.document | application/vnd.ms-excel | application/vnd.openxmlformats-officedocument.presentationml.presentation | txt';
    

    【讨论】:

      【解决方案4】:

      将以下内容添加到 mimes.php 文件中

      'binary/octet-stream'
      

      对于您想要支持的每种办公文档类型。不是一个理想的解决方案,但对我有用。

      【讨论】:

        猜你喜欢
        • 2015-11-06
        • 2019-03-01
        • 2015-06-05
        • 2019-01-22
        • 1970-01-01
        • 2021-02-18
        • 1970-01-01
        • 2012-05-12
        • 2020-02-05
        相关资源
        最近更新 更多