2017-08-01 55 views
0

我有2列表(圖片,圖片2)我的上傳表單適用於第一個文件輸入。圖片名稱記錄到列「pic」中,並將文件上傳到服務器。我如何擴展它來上傳2個文件?幫助請2個文件上傳輸入字段在一個表單中yii2

模式

公共$ upload_file;

public function uploadFile() { 
    // get the uploaded file instance 

    $image = UploadedFile::getInstance($this, 'upload_file'); 

    // if no image was uploaded abort the upload 
    if (empty($image)) { 
     return false; 
    } 
    $dir = Yii::getAlias('@frontend' . '/web/' . Yii::$app->params['fileUploadUrl']).$this->pic; 
    if (file_exists($dir)) { 
     unlink($dir); 
    } 
    // generate random name for the file 
    $this->pic = time(). '.' . $image->extension; 
    // the uploaded image instance 
    return $image; 
} 

public function getUploadedFile() { 
    // return a default image placeholder if your source avatar is not found 
    $pic = isset($this->pic) ? $this->pic : 'default.png'; 
    $dir = Yii::getAlias('@frontend' . '/web/' . Yii::$app->params['fileUploadUrl']); 
    if (!is_dir($dir)) { 
     FileHelper::createDirectory($dir); 
    } else { 
     return $dir . $pic;    
    } 

} 

在控制器動作

public function actionCreate() 
{ 
    $model = new Blog(); 

    if ($model->load(Yii::$app->request->post())) { 

     $upload_file = $model->uploadFile(); 

     if ($model->validate()) { 
      if($model->save()) { 

       if ($upload_file !== false) { 
        $path = $model->getUploadedFile(); 
        $upload_file->saveAs($path); 
       } 

       return $this->redirect(['view', 'id' => $model->id]); 
      } 

     } 
    } 

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

} 

視圖

<?= $form->field($model, 'upload_file')->widget(FileInput::classname(), [ 
    'options' => ['accept' => 'image/*'], 
]); ?> 
<?= $form->field($model, 'upload_file2')->widget(FileInput::classname(), [ 
    'options' => ['accept' => 'image/*'], 
]); ?> 

回答

0

找到了解決這種方式:)

一切都在控制器

public function actionRegistration() 
{ 
    $model = new Registration(); 
    $i = 0; 

    $imageArray = ['upload_file1','upload_file2','upload_file3','upload_file4']; 
    if ($model->load(Yii::$app->request->post())) { 

     foreach ($imageArray as $imageUpload) { 
      $i++; 
      $field_name = 'pic'.$i; 
      $image = UploadedFile::getInstance($model, $imageUpload); 
      // if no image was uploaded abort the upload 
      if (empty($image)) { 
       return false; 
      } 

      // generate random name for the file 
      $model->$field_name = 'pic'.$i.'_'. time(). '.' . $image->extension; 

      if ($image !== false) { 
       $pic = $model->$field_name ? $model->$field_name : 'default.png'; 
       $dir = Yii::getAlias('@frontend' . '/web/' . Yii::$app->params['fileUploadUrl']); 
       if (!is_dir($dir)) { 
        FileHelper::createDirectory($dir); 
       } 

       $image->saveAs($dir.$pic); 
      } 

     } 
     if (!$model->save()){ 
      var_dump($model->errors); 
     } 
     return $this->refresh(); 
    } 

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

你可以只寫相同的代碼'upload_file2'

$image2 = UploadedFile::getInstance($this, 'upload_file2'); 
... 

,或者您可以使用多個文件上傳控件的一些caind像that

0

如果您不需要上傳兩個單獨的輸入 - 你可以做以下 - 添加「MAXFILES」到模型的規則。

public function rules() { return [ [['upload_file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 2], ]; }

和uploadFile行動改變:

$image = UploadedFile::getInstance($this, 'upload_file');

到: $image = UploadedFile::getInstances($this, 'upload_file');

,最後在你的視野改變

<?= $form->field($model, 'upload_file')->widget(FileInput::classname(), [ 'options' => ['accept' => 'image/*'], ]); ?>

到:

<?= $form->field($model, 'upload_file[]')->widget(FileInput::classname(), [ 'options' => ['accept' => 'image/*', 'multiple' => true] , ]); ?>

+0

的問題是,我需要2個獨立fileinputs:C – Yury

+0

稀釋在這種情況下我會建議使用Kartiks FileInput小工具 – Kyle

+0

即時通訊使用它:)使用kartik \ file \ FileInput; – Yury

相關問題