2016-08-21 8 views
0

私はupdateとcreateのための_formビューを持っています。 zipファイルのみが許可されるファイル入力フィールドがあります。 zipがアップロードされると、モデル名に応じてフォルダに解凍する必要があります。Yii2。効果を得るためにファイルを2回アップロードする必要があるのはなぜですか?

問題は、最初に私に解凍されたフォルダを与えないので、2回アップロードする必要があります。

これはモデル

public function upload() 
{ 
    $this->uploadedFile = UploadedFile::getInstance($this, 'filepath'); 

    if (!empty($this->uploadedFile)) { 
     $slug = str_replace(' ', '-', strtolower(trim($this->name))); 
     $htmlPathRel = '/html/' . $slug . '/'; 
     $htmlPathAbs = Yii::getAlias('@frontend') . '/web' . $htmlPathRel; 

     // delete old files before extracting 
     @array_map('unlink', glob($htmlPathAbs . '/*.*')); 
     @rmdir($htmlPathAbs); 

     // unpack new files 
     $zip = new \ZipArchive; 
     if ($zip->open(Yii::getAlias('@frontend') . '/web/uploads/' . $this->uploadedFile->name) === true) { 
      $indexHtmlContent = $zip->getFromName('index.html'); 
      $order = new Order(); 
      $toSearch = array_filter($order->attributes(), function($value) { 
       return strpos($value, 'name="billing_') !== false; 
      }) + array('<form'); 

      if (!$indexHtmlContent) { 
       $this->addError('filepath', 'index.html not found in ' . $this->uploadedFile->name); 
       return false; 
      } 

      $notFound = []; 

      foreach ($toSearch as $string) { 
       if (strpos($indexHtmlContent, $string) === false) { 
        $notFound[] = $string; 
       } 
      } 

      if (!empty($notFound)) { 
       $this->addError('filepath', 'index.html doesn\'t contain required data: ' . implode(', ', $notFound)); 
       return false; 
      } 
      if (!is_dir($htmlPathAbs)) { 
       mkdir($htmlPathAbs); 
      } 
      $zip->extractTo($htmlPathAbs); 
      $zip->close(); 
     } 

     $this->filepath = $htmlPathRel . 'index.html'; 

     $this->save(); 

     if ($this->validate()) { 
      $this->uploadedFile->saveAs(Yii::getAlias('@frontend') . '/web/uploads/' . $this->uploadedFile->baseName . '.' . $this->uploadedFile->extension); 
      return true; 
     } else { 
      return false; 
     } 
    } else { 
     return false; 
    } 
} 

でのアップロード機能であり、これは、コントローラで作成したアクションを更新:

/** 
* Creates a new Html model. 
* If creation is successful, the browser will be redirected to the 'view' page. 
* @return mixed 
*/ 
public function actionCreate() 
{ 
    $model = new Html(); 

    if (Yii::$app->request->isPost) { 
     if ($model->load(Yii::$app->request->post())) { 
      Product::updateRoutes(); 
      $model->upload(); 
      $model->save(); 
      return $this->redirect(['view', 'id' => $model->id]); 
     } 
    } 

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

/** 
* Updates an existing Html model. 
* If update is successful, the browser will be redirected to the 'view' page. 
* @param string $id 
* @return mixed 
*/ 
public function actionUpdate($id) 
{ 
    $model = $this->findModel($id); 

    if (Yii::$app->request->isPost) { 
     $filepath = $model->filepath; 
     if ($model->load(Yii::$app->request->post())) { 
      Product::updateRoutes(); 
      if (!$model->upload()) { 
       $model->filepath = $filepath; 
      } 
      $model->save(); 
      return $this->redirect(['view', 'id' => $model->id]); 
     } 
    } 

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

私のコードが間違っていますか?

PS:コードリファクタリングのアイデアは大歓迎です! :)

答えて

1

このフォルダからファイルを解凍した後、ファイルを@frontend/web/uploadsに保存すると、そのファイルが存在するときにのみ動作します(2回目)。

関連する問題