1

This has come up before and I've followed this answer but no joy.

I'm trying to upload a .docx file to my CI app but it's giving me

The filetype you are attempting to upload is not allowed.

Now, when I vardump the mimetype of the received file by changing line 199 of system/libraries/upload.php to

$this->_file_mime_type($_FILES[$field]); die(var_dump($this->file_type));

I get

application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=binary

...and that IS listed in my application/config/mimes.php file as an acceptable mime for docx.

This being the case, what else could be wrong?

Mitya
  • 33,629
  • 9
  • 60
  • 107
  • are you specifiying the allowed types in the controller that takes the form input? Make sure you have the allowed types correctly set: `$config['allowed_types'] = 'docx|pdf|xlsx';` (modify to accept only the file types you want). The CI 2 File Upload Class could be helpful: https://www.codeigniter.com/userguide2/libraries/file_uploading.html – Javier Larroulet Sep 25 '18 at 12:40
  • have you set the extension as allowed type e.g. in your upload controller, just having it listed in mime types doesn't mean it's allowed to upload. set $config['allowed_types'] = 'gif|jpg|png|pdf|docx|doc'; – Loopo Sep 25 '18 at 12:41
  • Ah, good spot. Just added it... but it's still disallowing the .docx upload. Definitely listed in my `allowed_types` config param now... – Mitya Sep 25 '18 at 12:49
  • So it turns out that, by the time it got deep into CI's `system/libraries/uploads.php` script, the MIME had somehow morphed into `application/msword`. This *wasn't* in my mimes (and isn't even a valid .docx MIME, according to my cursory research). So I don't know how it got to that from the correct MIME earlier in the procedure. In any case, that MIME is now added to my list and it now works. – Mitya Sep 25 '18 at 15:41
  • 1
    @Utkanos could you post your last comment (and maybe some more insights) as an answer, thanks – Vickel Sep 25 '18 at 18:08
  • Done........... – Mitya Sep 26 '18 at 09:56

2 Answers2

1

It turns out that, somewhere along the CI flow, the mime had morphed from

application/vnd.openxmlformats-officedocument.wordprocessingml.document

to

application/msword

I have no idea at which point, or why, this happened. When I run the following in my CI controller method, I get the former.

$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, 'path/to/file.docx');
var_dump($mime);

So I've no idea how it changed to the latter along the way.

Needless to say, adding 'application/msword' to the allowed_types area of the upload options (passed to $this->upload->initialize($options)) solves the problem.

Mitya
  • 33,629
  • 9
  • 60
  • 107
0

I just recently had the same problem and lost all day to solve it, but without success. I recommend that you do not waste your time and just allow all the files and then do a php function to check if the file is .docx

stefo91
  • 618
  • 6
  • 16
  • Hmm, I can see why you'd do this but it's not an attractive solution. CI is correctly deriving the mime type, and that mime type is listed in its mimes whitelist, so it has to be a fault within CI somewhere. – Mitya Sep 25 '18 at 12:29