MusicManDev
MusicManDev

Reputation: 365

Yii2 Image upload on server throws an unexpected invalid configuration error

I am fairly new to yii and just started using Yii2 on my latest project(I am using Yii2 basic template btw). My problem right now is an upload image process wherein it is capable of multiple image uploads. On my localhost, it is fine, the images is successfully saved as well as the corresponding record accompanying it. However, the problem is when I have uploaded the website to my test server(linux server with PHP 5.4), the process is throwing an error:

Invalid Configuration
An internal server error occurred.

The above error occurred while the Web server was processing your request. Please contact us if you think this is a server error. Thank you.

I already made the necessary folders to have 777 permissions and still it wont work. I also tried to just create a record without uploading an image and as expected was successful which points directly to the upload process being the culprit.

Here is my code for my model:

class UploadImageForm extends Model
{
    /**
     * @var UploadedFile|Null file attribute
     */

    public $file;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['file'], 
              'file', 
              'maxFiles' => 10, 
              'extensions' => ['gif', 'jpg', 'png', 'jpeg', 'JPG', 'JPEG', 'PNG', 'GIF'],
              'maxSize' => 1024 * 1024 * 2
            ], // <--- here!
        ];
    }

}

Here is my code for the controller:

    public function actionCreate()
{
    $model = new Hotel();
    $uploadform = new UploadImageForm();
    $hotelcategory = HotelCategory::find()->all();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        $new_id = $model->hotel_id;

        #let us save the images
        $uploadform->file = UploadedFile::getInstances($uploadform, 'file');
        if ($uploadform->file && $uploadform->validate()) {
            foreach ($uploadform->file as $file) {

                $file->saveAs(Yii::$app->basePath .'/web/uploads/images' ."/". $file->baseName . '.' . $file->extension);
                $hotel_images = new HotelImages;
                $hotel_images->hotel_image_name = $file->baseName . '.' . $file->extension;
                $hotel_images->hotel_image_hotel = $new_id;
                $hotel_images->hotel_image_url = Yii::$app->urlManager->createUrl('/uploads/images')."/". $file->baseName . '.' . $file->extension;
                $hotel_images->save();

            }
        }            
        return $this->redirect(['view', 'id' => $model->hotel_id]);
    } else {
        return $this->render('create', [
            'model' => $model,
            'uploadform' => $uploadform,
            'hotelcategory' => $hotelcategory,
        ]);
    }
}

Can you point me to what I am doing wrong here?

Upvotes: 1

Views: 3201

Answers (3)

Nikolay Traykov
Nikolay Traykov

Reputation: 1695

I've had the same problem and I solved it by just enabling the extension in php.ini.

The Yii2 documentation clearly says:

getMimeType() throws yii\base\InvalidConfigException
when the fileinfo PHP extension is not installed and $checkExtension is false.

For more info click here

Hope this helps.

Upvotes: 0

Fernando Mauricio
Fernando Mauricio

Reputation: 11

Missing option checkExtensionByMimeType

        [['file'], 
          'file', 
          'maxFiles' => 10, 
          'extensions' => ['gif', 'jpg', 'png', 'jpeg', 'JPG', 'JPEG', 'PNG', 'GIF'],
          'checkExtensionByMimeType'=>false, // <--- here!
          'maxSize' => 1024 * 1024 * 2
        ], 
    ];
}

...

Upvotes: 1

ddsultan
ddsultan

Reputation: 2307

'Invalid Configuration' error while uploading a file could be related to 'The fileinfo PHP extension is not installed.'. Try to enable the extension in your php.ini file. After that your validation should also work. I hope this could help someone.

Upvotes: 1

Related Questions