2011-01-20 60 views
1

我在PHP中解壓縮zip文件並嘗試將其重命名爲content.txt。這裏是我的代碼:

if($this->copyFile($this->src,$this->dest)) { 
     $this->log .= "Successfully copied the file. Starting unzip.<br />"; 
     $res = $this->zip->open($this->dest); 
     if ($res === TRUE) { 
      $this->zip->extractTo("/htdocs/content-refresh/"); 
      $this->extracted = $this->zip->getNameIndex(0); 
      $this->log .= "Extracted ".$this->extracted." onto our server.<br />"; 
      if($this->zip->renameIndex(0,'content.txt')) { 
       $this->log .= "Renamed update file to content.txt.<br />"; 
      } else { 
       $this->log .= "Could not rename update file to content.txt.<br />"; 
      } 
      $this->zip->close(); 
      $this->log .= "The update file is ready to go. Now you can use the update functions.<br />"; 
     } else { 
      $this->log .= "Could not unzip the file.<br />"; 
     } 
    } 

這裏是文件輸出:

Successfully copied the file. Starting unzip. 

Extracted Hotel_All_Active 01-19-11.txt onto our server. 

Renamed update file to content.txt. 

The update file is ready to go. Now you can use the update functions. 

的問題是,它不重命名文件。我也嘗試過:

$this->zip->renameName(strval($this->extracted),'content.txt') 

但是,這也打印出來,它重命名文件,但沒有。我在這裏做錯了什麼,或者是這個功能越野車?

回答

3

renameIndex()功能用於重命名中的文件檔案。

看一下PHP手冊該功能的代碼,這是你可以看到它的修改存檔:

$zip = new ZipArchive; 
$res = $zip->open('test.zip'); 
if ($res === TRUE) { 
    $zip->renameIndex(2,'newname.txt'); 
    $zip->close(); 
} else { 
    echo 'failed, code:' . $res; 
} 

您需要使用rename()函數。

+0

謝謝!你是對的。 PHP.net文檔沒有很好地解釋。 – Jarred 2011-01-20 05:30:28