Jonnny
Jonnny

Reputation: 5039

Uploading .docx using mime types

I've been using the built-in mimeTypesvalidator for uploading files through Yii. I can't seem to get .docx files or .rtf files to be accepted though. Currently I have this code:

Model

array('file', 'file', 'on' => 'insert', 'safe'=>true, 'maxSize'=> 512000, 'maxFiles'=> 1, 
                    'mimeTypes' => 'application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, text/rtf, text/plain',
                    'tooLarge'=> 'File cannot be larger than 500KB.',
                    'wrongMimeType'=> 'Format must be:<code>.doc</code>  <code>.docx</code> <code>.txt</code> <code>.rtf</code>'), 

I've added the .docx extension to Yii's mimeTypes.php file with this line:

'docx'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 

I've also added this to my apache/conf/mime.type

application/vnd.openxmlformats-officedocument.wordprocessingml.document docx

I've also read around on the topic and seen that often these files are regarded as zips and added the mimeType application/octet-stream and application/zip and it still won't upload.

I've also echoed out the mime_type Yii is seeing and it matches the settings I have inside the model.

I'm just a bit lost on what else and where else to try and fix this. So any help from more experienced people would be great

Thanks

Jonny

Upvotes: 1

Views: 4939

Answers (1)

Michael H&#228;rtl
Michael H&#228;rtl

Reputation: 8607

If you look at the implementation of CFileValidator::validateFile() you'll notice that Yii will either use finfo_file() (since PHP 5.3.0) or mime_content_type() to find out the MIME type of your file.

So if you check your PHP version, you can debug further or supply your custom magic file.

Upvotes: 1

Related Questions