2011-01-20 110 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