Yii2如何使用自定义文件名保存文件?

问题描述 投票:0回答:1

如何使用自定义文件名保存文件?我使用gii generator为我的网站,我现在有一个文件输入我想要的是保存文件名称是从report_id得到的,这是我的primary key。例如,1_.docx 1是我的report_id。我还有一个doc_name字段,是的我可以保存我的文件并在doc_name获取名称。例如,sample.docx。但我的问题是当我使用report_id时,例如保存数据文件的名称只是.docx

这是我的控制器:

  public function actionCreate()
{
    $model = new reportDetails();
    if ($model->load(Yii::$app->request->post())) 
{      
    $project= $model->doc_name;
    $model->upload_file= UploadedFile::getInstance($model,'doc_file');
    $model->upload_file->saveAs('uploads/'.$project.'.'.$model->upload_file->extension);
    $model->doc_file='uploads/'.$project.'.'.$model->upload_file->extension;
    $model->doc_name=$project.'.'.$model->upload_file->extension;
    $model->save();

    Yii::$app->getSession()->setFlash('success','Data saved!');
    return $this->redirect(['view','id'=> $model->report_id]);
    } 

    else {

    return $this ->render('create', [
        'model'=>$model,
    ]);
    }         

}

正如你所看到的,$project=$model->doc_name;这个代码当我保存数据时,文件的名称将从doc_name获得,但如果我将其更改为report_id,当我保存它时结果是空白或仅.docx

这是我的_form

  <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
     &nbsp;&nbsp;&nbsp;<?= Html::button('<i class="fa fa-plus"></i>&nbsp;Add Person', ['value' => Url::to('index.php?r=name/create'), 'class' => 'btn btn-custom-pos btn-success', 'id' => 'officialsfor']) ?>
        </br>  
        </br> 
  <?php Pjax::begin(['id' => 'for_from']) ?>
      <div class="col-sm-6">
     <?= $form->field($model, 'doc_for')->widget(Select2::classname(), [
           'data' => ArrayHelper::map(Name::find()->asArray()->all(),
            'name_id',
            function($model, $defaultValue){
              return $model['position'].' '.$model['fname'].' '.$model['mname'].' '.$model['lname'];
            }),
            'language' => 'en',
            'options' => ['placeholder' => 'Choose a person ...'],
            'pluginOptions' => [
                'allowClear' => true,
                'width' => 500,
             ],
            ]); ?>
      </div>
      <div class="col-sm-6">
    <?= $form->field($model, 'doc_from')->widget(Select2::classname(), [
           'data' => ArrayHelper::map(Name::find()->asArray()->all(),
           'name_id',
            function($model, $defaultValue){
            return $model['position'].' '.$model['fname'].' '.$model['mname'].' '.$model['lname'];
            }),
            'language' => 'en',
            'options' => ['placeholder' => 'Choose a person ...'],
            'pluginOptions' => [
                'allowClear' => true,
                'width' => 500,
            ],
            ]); ?>
 <?php Pjax::end(); ?>
      </div>
        </br>
        </br></br></br>
      <div class="broder" style=" border-radius: 5px; padding: 12px; ">    
      </div>
  <div class="col-sm-6">
      <?= $form->field($model, 'user_id')->textInput(['type' => 'hidden','style'=>'width:500px;','placeholder' => 'Enter a Reference No....','value' =>ucfirst(Yii::$app->user->identity->first_name) . ' ' . ucfirst(Yii::$app->user->identity->middle_name) . ' ' . ucfirst(Yii::$app->user->identity->last_name)]) ?>
       <?= Html::activeLabel($model, 'user_id', ['label'=>ucfirst(Yii::$app->user->identity->first_name) . ' ' . ucfirst(Yii::$app->user->identity->middle_name) . ' ' . ucfirst(Yii::$app->user->identity->last_name),'style' => 'font-size: 21px;','class' => 'color']) ?>
      <br>
      <br>
      <?= $form->field($model, 'reference_no')->textInput(['style'=>'width:500px','placeholder' => 'Enter a Reference No....']) ?>
      <?= $form->field($model, 'subject')->textInput(['maxlength'=>true,'style'=>'width:500px','placeholder' => 'Enter a Subject....']) ?>
    <?= $form->field($model, 'doc_date')->widget(
        DatePicker::className(), [
            'inline' => false,
            'options' => ['placeholder' => 'Choose a Entry Date ...'],
            'clientOptions' => [
            'autoclose' => true,
            'format' => 'yyyy-mm-dd'
              ]
             ]);?>
  </div>
  <div class="col-sm-6" style="padding-top: 14px; ">
         </br>
         </br>
         </br></br>
    <?= $form->field($model, 'drawer_id')->textInput(['maxlength'=>true,'style'=>'width:500px','placeholder' => 'Enter a Drawer ID....', ]) ?>
    <?= $form->field($model, 'doc_name')->textInput(['maxlength'=>true,'style'=>'width:500px','placeholder' => 'Enter a Document Name....']) ?>
      <?= $form->field($model, 'doc_file')-> widget(
        FileInput::classname(),[
      'name' => 'doc_file',
      'options' => ['accept' => '.docx','.doc','.docs'],
      'pluginOptions' => [
          'showPreview' => false,
          'showCaption' => true,
          'showRemove' => true,
          'showUpload' => false
            ]
        ]);
      ?> 
  </div>
</div>
</br>
<div class="row">
  <div class="col-sm-12 text-center">
    <div class="form-group">
      <?= Html::submitButton($model->isNewRecord ? '<i class="fa fa-plus"></i>&nbsp;Create' : '<i class="fa fa-pencil"></i>&nbsp;Update', ['class' => $model->isNewRecord ? 'btn btn-success'  : 'btn btn-success','style' => 'padding:10px 60px;']) ?>
    </div>
  </div>
</div>
<?php ActiveForm::end(); ?>
yii yii2-advanced-app
1个回答
0
投票

正如你所说的report_id是主键,所以你不能在db中保存新记录之前得到它。

根据你的逻辑,插入正在发生新的记录。所以你需要更新你的代码,如下所示。核实。

public function actionCreate()
{
    $model = new reportDetails();
    if ($model->load(Yii::$app->request->post())) 
    {      
        $project= $model->doc_name;
        $model->upload_file = UploadedFile::getInstance($model,'doc_file');
        $model->upload_file->saveAs('uploads/'.$project.'.'.$model->upload_file->extension);
        $model->doc_file='uploads/'.$project.'.'.$model->upload_file->extension;
        $model->doc_name=$project.'.'.$model->upload_file->extension;
        $model->save();

        // after save you will get report_id
        rename('uploads/'.$project.'.'.$model->upload_file->extension, 'uploads/'.$model->report_id.'.'.$model->upload_file->extension);
        $model->doc_file='uploads/'.$model->report_id.'.'.$model->upload_file->extension;
        $model->doc_name=$model->report_id.'.'.$model->upload_file->extension;
        $model->save();


        Yii::$app->getSession()->setFlash('success','Data saved!');
        return $this->redirect(['view','id'=> $model->report_id]);
    } 

    else {

        return $this ->render('create', [
            'model'=>$model,
        ]);
    }         

}
© www.soinside.com 2019 - 2024. All rights reserved.