开发者

$zip Rename Functions Not Working in PHP

开发者 https://www.devze.com 2023-02-05 21:45 出处:网络
I am extracting a zip file in PHP and trying to rename it to content.txt. Here is my code: if($this->copyFile($this->src,$this->dest)) {

I am extracting a zip file in PHP and trying to rename it to content.txt. Here is my code:

    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 />";
        }
    }

Here is the file output:

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.

The problem is that it does not rename the file. I have also tried:

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

But that also prints out that it renamed the file, but does not. Am I doing something wrong here, or is this function buggy?


The renameIndex() function is for renaming a file inside an archive.

Looking at the code in the PHP Manual for that function, it's you can see it's modifying the archive:

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

You need to use the rename() function instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消