简体   繁体   中英

Yii2: how to upload two images from two fields in form

Create Function from the controller:

$imageName = $model->primary_name;
// get the uploaded file instance. for multiple file uploads  
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs('uploads/'.$imageName.'.'.$model->file->extension);          
//save the path in db
$model->id_image = 'uploads/'.$imageName.'.'.$model->file->extension;

$imageName1 = $model->adult_2_name;
// get the uploaded file instance. for multiple file uploads
$model->file2 = UploadedFile::getInstance($model, 'file2');
$model->file2->saveAs('uploads/adult_2/'.$imageName1.'.'.$model->file2->extension);
//save the path in db
$model->id_image_2 = 'uploads/adult_2/'.$imageName1.'.'.$model->file2->extension;

$model->save();

Model

public $file;
public $file2;

[['file','file2'],'file'],
[['id_image', 'id_image_2'], 'string', 'max' => 500],

View

     <?php

            use yii\helpers\Html;
            /*use yii\widgets\ActiveForm;*/
            use kartik\form\ActiveForm;
            use kartik\file\FileInput;
            use yii\helpers\Url;
            use yii\bootstrap\Modal;

            /* @var $this yii\web\View */
            /* @var $model backend\models\CreateBookings */
            /* @var $form yii\widgets\ActiveForm */
            ?>

 <div class="create-bookings-form">

<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data'] ]); ?>

<?= Html::activeHiddenInput($model, 'booking_id', ['value' => '' ]) ?>
    <?= $form->field($model, 'check_in')->label(false)->input('date', ['placeholder' => '']) ?>

    <?= $form->field($model, 'check_out')->label(false)->input('date', ['placeholder' => '']) ?>

    <?= $form->field($model, 'room_type')->label(false)->input('text', ['placeholder' => 'Select Room Type...']) ?>
    <?= $form->field($model, 'primary_name')->label(false)->textInput(['maxlength' => true]) ?>
    <?php $list = ['Male' => 'Male', 'Female' => 'Female'];
    echo $form->field($model, 'gender_1')->label(false)->radioList($list, ['inline'=>true]); ?>
    <?php   echo $form->field($model, 'primary_mobile', ['feedbackIcon' => ['default' => 'phone']])->label(false)->textInput(['placeholder'=>'Enter phone number...']); ?>
    <?php   echo $form->field($model, 'primary_email', [
                            'feedbackIcon' => [
                            'default' => 'envelope',
                            'success' => 'ok',
                            'error' => 'exclamation-sign',
                            'defaultOptions' => ['class'=>'text-primary']
                                ]
                                ])->label(false)->textInput(['placeholder'=>'Enter a valid email address...']); ?>

    <?php echo $form->field($model, 'id_type')->label(false)->dropDownList(['Passport' => 'Passport', 'Aadhar Card' => 'Aadhar Card', 'Driving Licence' => 'Driving Licence','Voter ID' => 'Voter ID',]); ?>
    <?= $form->field($model, 'id_number')->label(false)->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'file')->label(false)->fileInput(['id'=>'imgfile','onchange' => 'readURL(this)']); ?>                  
    <?php echo '<img id="preview" src="#" width="100" height="100" alt="your image" />' ; ?>                 
    <?= $form->field($model, 'adult_2_name')->textInput(['maxlength' => true]) ?>
    <?= $form->field($model, 'file')->fileInput(
                    [
                    'id'=>'imgfile2',
                    'onchange' => 'readURL(this)'
                    ]); 
                ?>
    <?= '<img id="preview2" src="#" width="100" height="100" alt="your image" />' ?>

    <div class="form-group">
                    <?= Html::submitButton($model->isNewRecord ?  'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
                </div>

                <?php ActiveForm::end(); ?>

            </div>

            <?php
            $script = <<< JS

            $("#imgfile").change(function(){
                if (this.files && this.files[0]) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        $('#preview').attr('src', e.target.result);
                    }
                    reader.readAsDataURL(this.files[0]);
                }
            });

            JS;
            $this->registerJs($script);
            ?>
            <?php
            $script = <<< JS

            $("#imgfile2").change(function(){
                if (this.files && this.files[0]) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        $('#preview2').attr('src', e.target.result);
                    }
                    reader.readAsDataURL(this.files[0]);
                }
            });

            JS;
            $this->registerJs($script);
            ?>

Trying to upload two images in database but not able to make it.

Getting error:

Call to a member function saveAs() on string

Updated the Active Form....................

change in this line :

$model->file = UploadedFile::getInstance($model, 'file2');

$model->file to $model->file2


UPDATE :

and also on your view file, the second input :

<?= $form->field($model, 'file')->fileInput(
    [ 
      'id'=>'imgfile2',
      'onchange' => 'readURL(this)'
    ]); 
?>

should be related to file2 otherwise you'll be overriding the first uploaded file :

<?= $form->field($model, 'file2')->fileInput(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM