Yii2如何保存相同的文件名但文件或内容不同?

时间:2017-06-06 01:39:10

标签: yii2 yii2-advanced-app

如何保存相同名称但内容不同,例如samplename.docx。现在我想再次保存,但结果相同。当我创建一个新报告并使用名称samplename时,它将覆盖另一个相同的名称。因此在uploads文件夹中,samplename.docx只有一个。但是在dgView中不止一个。

这是我的控制者:

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,
        ]);
    }
}

这是我的模特:

public $upload_file;
public function rules()
{
    return [
        [['reference_no', 'subject', 'doc_for', 'doc_from', 'doc_date', 'doc_file', 'doc_name', 'drawer_id'], 'required'],
        [['doc_date'], 'safe'],
        [['drawer_id'], 'integer'],
        [['reference_no', 'doc_for', 'doc_from'], 'string', 'max' => 200],
        [['subject'], 'string', 'max' => 255],
        [['doc_file', 'doc_name'], 'string', 'max' => 50],
        [['user_id'],'string'],

    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'report_id' => 'Report ID',
        'reference_no' => 'Reference No:',
        'subject' => 'Subject:',
        'doc_for' => 'For:',
        'doc_from' => 'From:',
        'doc_date' => 'Document Entry Date:',
        'doc_file' => 'Document File:',
        'doc_name' => 'Document Name:',
        'drawer_id' => 'Drawer ID:',
        'user_id' => 'Encoder Name:',
    ];
}

这是我的表格:

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

2 个答案:

答案 0 :(得分:0)

使用

if(file_exists('...')){
   // upload file with , new file name
}
else{
   // upload file 
}

答案 1 :(得分:0)

试试这个

if (file_exists($fileFullPath)) { 
 unlink ($fileFullPath); 
 echo "delete existing file creating new <br />";
 }else{
  echo "file not exists creating new <br />"; 
}