创建功能:
$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],
查看
<?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);
?>
尝试在数据库中上传两张图片但无法上传。
获取错误:
在字符串
上调用成员函数saveAs()
更新了活动表格....................
答案 0 :(得分:0)
更改此行:
$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(