How do I upload .docx using Codeigniter? (PHP)

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 09:43:33

问题


I'm attempting to upload a .docx using the upload helper in Codeigniter.

public function upload($id,$type){
        $folder = $type;
        $config['upload_path'] = RESOURCE_PATH . $folder;
        $config['allowed_types'] = 'pdf|doc|docx';
        $config['max_size'] = '100000';
        $this->CI->load->library('upload',$config);
        //$this->load->library('upload', $config);
        if (!$this->CI->upload->do_upload('userfile')){
            echo $this->CI->upload->display_errors();
        }
 ....

The upload function works fine on my local host. However, when I try uploading the .docx file on the server I get "The filetype you are attempting to upload is not allowed".

I can upload other files on the server and the code is an exact copy of what I have locally. I also googled around and found some people changed the mime for .docx to:

'docx'  =>  array('application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/octet-stream')  

but that doesn't seem to have resolved the issue. What could be going wrong and how do I resolve the issue so I can resolve this issue?


回答1:


Try to add this mime type: application/zip to application/config/mimes.php for docx. It works for me. If it not help. Open system/libraries/Upload.php(line 205) and do this:

var_dump($this->file_type);
exit();

You will see file type of you .docx and need to add it to docx mimes list in config.




回答2:


Open the application/config/mimes.php file and change the mime types array of doc and docx with the following mime types.

'doc'  => array('application/msword', 'application/octet-stream'),
'docx' => array('application/msword', 'application/octet-stream','application/vnd.openxmlformats-officedocument.wordprocessingml.document'),

Just replace doc and docx elements value with the above mime types array.



来源:https://stackoverflow.com/questions/18234151/how-do-i-upload-docx-using-codeigniter-php

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