PHP Zip Archive - open 或 extractTo 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11401706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:27:28  来源:igfitidea点击:

PHP Zip Archive - open or extractTo is not working

phpzip

提问by Lawrence DeSouza

I'm completely at a loss for explaining why this isn't working. HELP!

我完全无法解释为什么这不起作用。帮助!

$archive = "x.zip";
$zip = new ZipArchive();
$res = $zip->open($archive);

if ($res === 'TRUE') {
    $unzip_success= $zip->extractTo('/temp/', "inscriptions.txt")

    $zip->close();
}
  • the target dir "temp" is "0777" permissions
  • the code obtained from $res is "11" and not "TRUE" as required by the documentation on PHP.net
  • note: must put the full url for $archive and the first argument of extractTo
  • 目标目录“temp”是“0777”权限
  • 从 $res 获得的代码是“11”而不是 PHP.net 上的文档要求的“TRUE”
  • 注意:必须把 $archive 的完整 url 和 extractTo 的第一个参数

采纳答案by Hardik

if nothing works then check if your server is linux. if its linux you can run unzip command to unzip your file via php's system/exec function. i.e

如果没有任何效果,请检查您的服务器是否为 linux。如果是 linux,您可以运行 unzip 命令通过 php 的 system/exec 函数解压缩您的文件。IE

system("unzip archive.zip");

to extract specific file you can check man docs for unzip. many times due to server parameters zip library doesn't work as expected in that cases i switch back to linux commands.

要提取特定文件,您可以检查 man docs 以进行解压缩。很多时候由于服务器参数 zip 库不能按预期工作,在这种情况下我切换回 linux 命令。

回答by Alec

ZipArcive::extractTo is case-sensitive. If file's name to be extracted do not meet zipped one exactly, the method returns false.

ZipArcive::extractTo 区分大小写。如果要提取的文件名与压缩文件名不完全一致,则该方法返回 false。

回答by Senanayaka

I faced the same problem, I have fixed this :) Use $_SERVER['DOCUMENT_ROOT']for url. My code (codeigniter):

我遇到了同样的问题,我已经解决了这个问题:) $_SERVER['DOCUMENT_ROOT']用于 url。我的代码(codeigniter):

$this->load->library('unzip');
$file = $this->input->GET('file');
$this->unzip->extract($_SERVER['DOCUMENT_ROOT'].'/TRAS/application/uploads/' .    $file,$_SERVER['DOCUMENT_ROOT'].'/TRAS/application/views/templates/' . $file);

回答by Diego Agulló

The problem is that you are quoting TRUE, which is a keyword and should be left without single quotes. Plus, you could check if the file exists in the zip archive prior to its extraction with locateName:

问题是你在引用TRUE,这是一个关键字,应该没有单引号。另外,您可以在使用locateName提取文件之前检查该文件是否存在于 zip 存档中:

$archive = "x.zip";
$zip = new ZipArchive();
$res = $zip->open($archive);

if ($res === true && $zip->locateName('inscriptions.txt') !== false) {
    $unzip_success= $zip->extractTo('/tmp/', "inscriptions.txt");

    $zip->close();
}

回答by keithhatfield

If $resis equal to 11, that means that ZipArchivecan't open the specified file.

如果$res等于11,则表示ZipArchive无法打开指定的文件。

To test this:

要测试这个:

$archive = "x.zip";
$zip = new ZipArchive();
$res = $zip->open($archive);

if($res == ZipArchive::ER_OPEN){
    echo "Unable to open $archive\n";
}

回答by Thunderstick

Adding document root is what worked for me as well. here is my code

添加文档根目录也对我有用。这是我的代码

$zip = new ZipArchive;
        if ($zip->open($_SERVER['DOCUMENT_ROOT'].'/'.$folder.$file_path) === TRUE) {
            $zip->extractTo($_SERVER['DOCUMENT_ROOT'].'/$folder');
            $zip->close();
            echo 'ok';
        }

回答by Galley

I meet same problem, but I can open the zip file, it returns trueafter open.

我遇到了同样的问题,但是我可以打开 zip 文件,true打开后它会返回。

My issue is I got false after $zip->extractTo().

我的问题是我在$zip->extractTo().

I finally success after delete files named in CHINESE (NO-ENGILISH) in the zip file.

在 zip 文件中删除以中文(无英文)命名的文件后,我终于成功了。

回答by Bob Ray

I had the same problem on Windows 10. The only solution I found was just to try extractTo twice, even though the open() was successful:

我在 Windows 10 上遇到了同样的问题。我找到的唯一解决方案是尝试 extractTo 两次,即使 open() 成功了:

$zip = new ZipArchive;
if ($open === true) {
    $result = $zip->extractTo($destination);
    if ($result === false) {
        $result = $zip->extractTo($destination);
    }
    $zip->close();
}

The fact that the second extractTo() works (with no intervening action) would seem to indicate that there's nothing wrong with the archive or the destination directory.

第二个extractTo() 工作(没有干预操作)的事实似乎表明存档或目标目录没有任何问题。