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

标签 php mysql yii2

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

型号

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);
            ?>

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

出现错误:

Call to a member function saveAs() on string

更新了事件表单......................

最佳答案

此行的更改:

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

$model->file$model->file2

<小时/>

更新:

也在您的 View 文件上,第二个输入:

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

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

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

关于php - Yii2:如何从表单中的两个字段上传两个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31766263/

相关文章:

php - 为什么 $_GET = index.php

php - 改变 Imagerotate 的旋转中心

mysql - QSqlQuery::prepare + MySQL ODBC 连接器

MySql 结果集前后的第一个结果

php - 在模型中获取值使用 asArray Yii2

php - 每页查询太多

PHP 7 RC3 : How to install missing MySQL PDO

php - 在 MySQL LEFT JOIN 显示问题中需要帮助

python - 从字典创建 SQL 查询

javascript - javascript 数组中有新行时出错