Yii2:如何在表单中从两个字段上传两个图像


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

从控制器创建函数:

$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();

public $file;
public $file2;
[['file','file2'],'file'],
[['id_image', 'id_image_2'], 'string', 'max' => 500],
<<p> 视图/strong>
     <?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);
            ?>

尝试在数据库中上传两个图像,但无法成功。

得到错误:

在string

上调用成员函数savea ()

更新活动形式 ....................

这一行的变化:

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

$model->file$model->file2


:

以及在视图文件中,第二个输入:

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

应该与file2相关,否则你将覆盖第一个上传的文件:

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