2016-08-21 52 views
0

我有一个_form视图进行更新和创建。有文件输入字段,只允许zip文件。上传zip文件时,应根据型号名称将其解压到一个文件夹。Yii2。为什么我需要上传两次文件才能产生效果?

问题是我必须上传它两次才能有效果,因为第一次不给我解压缩文件夹。

这是在型号

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,以便它仅在文件存在时有效 - 第二次。

相关问题