title: Yii2 How to save file with custom file name?
body: <p>How to save a file using a custom file name? I used <code>gii generator</code> for my website and i have a file input in my form now i want is after saving the file the name is get from <code>report_id</code> which is my <code>primary key</code>. For example, <code>1_.docx</code> the 1 is my <code>report_id</code>. I have also a <code>doc_name</code> field, Yes i can save my file and get the name in <code>doc_name</code>. For example, <code>sample.docx</code>. But my problem is when i used the <code>report_id</code> and for example save the data the name of the file is <code>.docx</code> only.</p>
  
  <p>This is my controller:</p>
  
  <pre><code>  public function actionCreate()
  {
      $model = new reportDetails();
      if ($model-&gt;load(Yii::$app-&gt;request-&gt;post())) 
  {      
      $project= $model-&gt;doc_name;
      $model-&gt;upload_file= UploadedFile::getInstance($model,'doc_file');
      $model-&gt;upload_file-&gt;saveAs('uploads/'.$project.'.'.$model-&gt;upload_file-&gt;extension);
      $model-&gt;doc_file='uploads/'.$project.'.'.$model-&gt;upload_file-&gt;extension;
      $model-&gt;doc_name=$project.'.'.$model-&gt;upload_file-&gt;extension;
      $model-&gt;save();
  
      Yii::$app-&gt;getSession()-&gt;setFlash('success','Data saved!');
      return $this-&gt;redirect(['view','id'=&gt; $model-&gt;report_id]);
      } 
  
      else {
  
      return $this -&gt;render('create', [
          'model'=&gt;$model,
      ]);
      }         
  
  }
  </code></pre>
  
  <p>As you can see, <code>$project=$model-&gt;doc_name;</code> this the codes when i save a data, the name of the file will get from the <code>doc_name</code> but if i change it into <code>report_id</code>, when i save it the result is blank or <code>.docx</code> only.</p>
  
  <p>This is my <code>_form</code>:</p>
  
  <pre><code>  &lt;?php $form = ActiveForm::begin(['options' =&gt; ['enctype' =&gt; 'multipart/form-data']]); ?&gt;
       &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;?= Html::button('&lt;i class="fa fa-plus"&gt;&lt;/i&gt;&amp;nbsp;Add Person', ['value' =&gt; Url::to('index.php?r=name/create'), 'class' =&gt; 'btn btn-custom-pos btn-success', 'id' =&gt; 'officialsfor']) ?&gt;
          &lt;/br&gt;  
          &lt;/br&gt; 
    &lt;?php Pjax::begin(['id' =&gt; 'for_from']) ?&gt;
        &lt;div class="col-sm-6"&gt;
       &lt;?= $form-&gt;field($model, 'doc_for')-&gt;widget(Select2::classname(), [
             'data' =&gt; ArrayHelper::map(Name::find()-&gt;asArray()-&gt;all(),
              'name_id',
              function($model, $defaultValue){
                return $model['position'].' '.$model['fname'].' '.$model['mname'].' '.$model['lname'];
              }),
              'language' =&gt; 'en',
              'options' =&gt; ['placeholder' =&gt; 'Choose a person ...'],
              'pluginOptions' =&gt; [
                  'allowClear' =&gt; true,
                  'width' =&gt; 500,
               ],
              ]); ?&gt;
        &lt;/div&gt;
        &lt;div class="col-sm-6"&gt;
      &lt;?= $form-&gt;field($model, 'doc_from')-&gt;widget(Select2::classname(), [
             'data' =&gt; ArrayHelper::map(Name::find()-&gt;asArray()-&gt;all(),
             'name_id',
              function($model, $defaultValue){
              return $model['position'].' '.$model['fname'].' '.$model['mname'].' '.$model['lname'];
              }),
              'language' =&gt; 'en',
              'options' =&gt; ['placeholder' =&gt; 'Choose a person ...'],
              'pluginOptions' =&gt; [
                  'allowClear' =&gt; true,
                  'width' =&gt; 500,
              ],
              ]); ?&gt;
   &lt;?php Pjax::end(); ?&gt;
        &lt;/div&gt;
          &lt;/br&gt;
          &lt;/br&gt;&lt;/br&gt;&lt;/br&gt;
        &lt;div class="broder" style=" border-radius: 5px; padding: 12px; "&gt;    
        &lt;/div&gt;
    &lt;div class="col-sm-6"&gt;
        &lt;?= $form-&gt;field($model, 'user_id')-&gt;textInput(['type' =&gt; 'hidden','style'=&gt;'width:500px;','placeholder' =&gt; 'Enter a Reference No....','value' =&gt;ucfirst(Yii::$app-&gt;user-&gt;identity-&gt;first_name) . ' ' . ucfirst(Yii::$app-&gt;user-&gt;identity-&gt;middle_name) . ' ' . ucfirst(Yii::$app-&gt;user-&gt;identity-&gt;last_name)]) ?&gt;
         &lt;?= Html::activeLabel($model, 'user_id', ['label'=&gt;ucfirst(Yii::$app-&gt;user-&gt;identity-&gt;first_name) . ' ' . ucfirst(Yii::$app-&gt;user-&gt;identity-&gt;middle_name) . ' ' . ucfirst(Yii::$app-&gt;user-&gt;identity-&gt;last_name),'style' =&gt; 'font-size: 21px;','class' =&gt; 'color']) ?&gt;
        &lt;br&gt;
        &lt;br&gt;
        &lt;?= $form-&gt;field($model, 'reference_no')-&gt;textInput(['style'=&gt;'width:500px','placeholder' =&gt; 'Enter a Reference No....']) ?&gt;
        &lt;?= $form-&gt;field($model, 'subject')-&gt;textInput(['maxlength'=&gt;true,'style'=&gt;'width:500px','placeholder' =&gt; 'Enter a Subject....']) ?&gt;
      &lt;?= $form-&gt;field($model, 'doc_date')-&gt;widget(
          DatePicker::className(), [
              'inline' =&gt; false,
              'options' =&gt; ['placeholder' =&gt; 'Choose a Entry Date ...'],
              'clientOptions' =&gt; [
              'autoclose' =&gt; true,
              'format' =&gt; 'yyyy-mm-dd'
                ]
               ]);?&gt;
    &lt;/div&gt;
    &lt;div class="col-sm-6" style="padding-top: 14px; "&gt;
           &lt;/br&gt;
           &lt;/br&gt;
           &lt;/br&gt;&lt;/br&gt;
      &lt;?= $form-&gt;field($model, 'drawer_id')-&gt;textInput(['maxlength'=&gt;true,'style'=&gt;'width:500px','placeholder' =&gt; 'Enter a Drawer ID....', ]) ?&gt;
      &lt;?= $form-&gt;field($model, 'doc_name')-&gt;textInput(['maxlength'=&gt;true,'style'=&gt;'width:500px','placeholder' =&gt; 'Enter a Document Name....']) ?&gt;
        &lt;?= $form-&gt;field($model, 'doc_file')-&gt; widget(
          FileInput::classname(),[
        'name' =&gt; 'doc_file',
        'options' =&gt; ['accept' =&gt; '.docx','.doc','.docs'],
        'pluginOptions' =&gt; [
            'showPreview' =&gt; false,
            'showCaption' =&gt; true,
            'showRemove' =&gt; true,
            'showUpload' =&gt; false
              ]
          ]);
        ?&gt; 
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;/br&gt;
  &lt;div class="row"&gt;
    &lt;div class="col-sm-12 text-center"&gt;
      &lt;div class="form-group"&gt;
        &lt;?= Html::submitButton($model-&gt;isNewRecord ? '&lt;i class="fa fa-plus"&gt;&lt;/i&gt;&amp;nbsp;Create' : '&lt;i class="fa fa-pencil"&gt;&lt;/i&gt;&amp;nbsp;Update', ['class' =&gt; $model-&gt;isNewRecord ? 'btn btn-success'  : 'btn btn-success','style' =&gt; 'padding:10px 60px;']) ?&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;?php ActiveForm::end(); ?&gt;
  </code></pre>