.xlsx file upload mime type error Yii2




// Merchant attachment model 

<?php


namespace app\models;


use Yii;


/**

 * This is the model class for table "merchant_attachments".

 *

 * @property integer $id

 * @property string $name

 * @property string $description

 * @property string $path

 * @property string $type

 * @property string $created_dt

 */

class MerchantAttachments extends \yii\db\ActiveRecord

{

    //public $images;

    public $documents;

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'merchant_attachments';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['created_dt'], 'required'],

            [['created_dt'], 'safe'],

            [['name', 'description', 'path'], 'string', 'max' => 255],

            [['type'], 'string', 'max' => 45],            

            [

                'documents',

                'file',

                //'extensions' => ['rtf', 'doc', 'docx', 'pdf','ppt','txt'],

                'mimeTypes'  => ['application/msword',

                    'application/vnd.openxmlformats-officedocument.wordprocessingml.document',

                    'application/pdf',

                    'application/vnd.ms-excel',

                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',

                    'text/plain',

                    'application/rtf','application/x-rtf','text/rtf','text/richtext',

                    'text/csv',

                    'application/csv'],

                

                'wrongMimeType'=>Yii::t('app','Only files with these extensions are allowed: rtf, doc, docx, pdf, xls, xlsx, csv, txt.'),

                'maxSize'    => 1024 * 1024 * 2,

                'tooBig' => Yii::t('app','File exceeds 2MB max size'),

                

                //'maxFiles'   => 10

            ],

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => Yii::t('app', 'ID'),

            'name' => Yii::t('app', 'Name'),

            'description' => Yii::t('app', 'Description'),

            'path' => Yii::t('app', 'Path'),

            'type' => Yii::t('app', 'Type'),

            'created_dt' => Yii::t('app', 'Created Dt'),

        ];

    }

}





/** upload controller 

     * upload merchant attachments

     */

    function actionUploadmerchantdoc($id = "") {


        if (isset($_FILES['MerchantAttachments']['name']['documents']) && count($_FILES['MerchantAttachments']['name']['documents'])) {

            Yii::$app->params['uploadPath'] = Yii::$app->params['MERCHANT_PHOTO_DIR'];

            Yii::$app->params['uploadUrl'] = Yii::$app->params['MERCHANT_PHOTO_URL'];


            yii\helpers\Utils::makeDirectory(Yii::$app->params['uploadPath']);


            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;


            $attachments = new MerchantAttachments();


            $images = UploadedFile::getInstances($attachments, 'documents');

           

            foreach ($images as $key => $file) {

                $output[$key] = array();


                $attachment = new MerchantAttachments();

                $baseName = \yii\helpers\Utils::imageNameFilter($file->baseName);

                $extension = '.' . $file->extension;

                $fileName = $baseName . $extension;

                $attachment->name = $fileName;

                $attachment->path = $fileName;

                $attachment->type = 'document';

                $attachment->documents = $file;

                $attachment->created_dt = Yii::$app->formatter->asDateTime('now', 'php:Y-m-d H:i:s');


                if ($attachment->validate()) {

                    $attachment->save();


                    $fileName = $attachment->id . $extension;

                    $attachment->path = $fileName;

                    $attachment->save();

                    $file->saveAs(Yii::$app->params['uploadPath'] . $fileName);


                    $attachmentArray = MerchantAttachments::find()->asArray()->where(['id' => $attachment->id])->one();

                    $output[$key]['url'] = Yii::$app->params['uploadUrl'] . $fileName;

                    $output[$key]['success'] = true;

                    $output[$key]['message'] = Yii::t('app', 'Success') . ':' . $file->name . " " . Yii::t('app', 'Popupmessage:Uploaded successfully');

                    $output[$key]['output'] = $this->renderPartial('@app/modules/merchant/views/merchant/_merchant_document', array('document' => $attachmentArray));

                } elseif ($attachment->hasErrors()) {

                    $errors = $attachment->getErrors('documents');

                    $output[$key]['success'] = false;

                    $output[$key]['message'] = $errors[0];

                } else {

                    $output[$key]['success'] = false;

                    $output[$key]['message'] = Yii::t('app', 'Error : The file') . '&nbsp"' . $file->name . '"&nbsp' . Yii::t('app', 'Is too big . its size cannot exceed 2MB');

                }

            }

            return $output;

        }

    }



error after generating file in linux os with .xlsx file type. the mime type i have added fro .xlsx is not supporting and generating error.

please help me to solve this problem same error for text file in linux Operating system

There is a symbol <> above the textArea where you write this comment. Click on that and you can display your code in a readable format.


	Like this!