2015-04-21 130 views
2

我按照以下指南https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md,但沒有文件上傳。下面是我的配置yii2文件上傳不起作用

_form

這是一個循環,所以我可以得到不同的記錄數組。

<?= $form->field(new UploadForm , "[$count]file")->fileInput()->label(false) ?> 

視圖

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>... 

控制器

if(isset(Yii::$app->request->post()['Factsheets'])){ 

       for($i=0 ; $i < count(Yii::$app->request->post()['Factsheets']); $i++) { 
        //Yii::error(print_r(Yii::$app->request->post()['Factsheets'][$i],true)); 

        if(!empty(Yii::$app->request->post()['UploadForm'][$i]['file'])){ 
         $file = new UploadForm(); 
         $file->file = UploadedFile::getInstance(Yii::$app->request->post()['UploadForm'][$i], 'file'); 

         if ($file->file && $file->validate()) { 
          $file->file->saveAs('uploads/' . $file->file->baseName . '.' . $file->file->extension); 
         } 
        } 
       }    
      } 

交日誌

[Factsheets] => Array 
    (
     [0] => Array 
      (
       [type] => image 
       [factsheet_id] => 1185 
       [path] => ../public/filespool/2/289/Pelotas_Reprocessing.jpg 
      ) 

     [1] => Array 
      (
       [type] => tech_paper 
       [factsheet_id] => 1433 
       [path] => ?basin=pelotas 
      ) 

     [2] => Array 
      (
       [type] => factsheet 
       [factsheet_id] => 1844 
       [path] => ../public/filespool/2/289/Pelotas_Reprocessing.pdf 
      ) 

    ) 

[UploadForm] => Array 
    (
     [0] => Array 
      (
       [file] => 
      ) 

     [1] => Array 
      (
       [file] => 
      ) 

     [2] => Array 
      (
       [file] => 
      ) 

    ) 

我在註冊日誌中注意到了以下內容。我如何構建它?

$_FILES = [ 
    'UploadForm' => [ 
     'name' => [ 
      0 => [ 
       'file' => 'Destin_Dome.jpg' 
      ] 
      1 => [ 
       'file' => '' 
      ] 
      2 => [ 
       'file' => 'Pelotas_Reprocessing.pdf' 
      ] 
     ] 
     'type' => [ 
      0 => [ 
       'file' => 'image/jpeg' 
      ] 
      1 => [ 
       'file' => '' 
      ] 
      2 => [ 
       'file' => '' 
      ] 
     ] 
     'tmp_name' => [ 
      0 => [ 
       'file' => '/tmp/phpoPgbJ9' 
      ] 
      1 => [ 
       'file' => '' 
      ] 
      2 => [ 
       'file' => '' 
      ] 
     ] 
     'error' => [ 
      0 => [ 
       'file' => 0 
      ] 
      1 => [ 
       'file' => 4 
      ] 
      2 => [ 
       'file' => 1 
      ] 
     ] 
     'size' => [ 
      0 => [ 
       'file' => 1129373 
      ] 
      1 => [ 
       'file' => 0 
      ] 
      2 => [ 
       'file' => 0 
      ] 
     ] 
    ] 
] 

現在看來似乎是驗證失敗,但是我能做出這樣的副本:

if(!empty($_FILES['UploadForm']['tmp_name']['file'])){ 
         copy($_FILES['UploadForm']['tmp_name']['file'],"/tmp/".$_FILES['UploadForm']['name']['file']); 
} 

的FileUpload模型

public $file; 
    public $image; 
    public $factsheet; 


    /** 
    * @return array the validation rules. 
    */ 
    public function rules() 
    { 
     return [ 
      [['file'], 'file','maxFiles' => 10], 
      [['image'], 'file','extensions' => 'gif, jpg'], 
      [['factsheet'], 'file','checkExtensionByMimeType' => false,'extensions' => 'pdf'], 
     ]; 
    } 

回答

1

我可能是錯的,但我認爲這個問題是在您的控制器中:您的模型規則也適用於多個文件上傳和您的表單字段,但在控制器中,您將$model->file設置爲單個UploadedFile實例。這就是爲什麼你的驗證不起作用。我不能給你的建議(改變控制器或模型規則),因爲我不清楚你在這裏做什麼。它看起來像你從單個文件上傳字段創建多個模型...

+0

我想上傳多個文件 – shorif2000