$zip 重命名函数在 PHP 中不起作用

发布于 2024-10-12 14:48:47 字数 1503 浏览 5 评论 0原文

我正在 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')

但这也打印出它重命名了文件,但没有。我在这里做错了什么,还是这个函数有问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以心动 2024-10-19 14:48:47

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() 函数代替。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文