开发者

ZipArchive::getFromName does not find filename

Any idea what I am doing wrong here? It keeps dying with 'bye bye'. There is an index.php file inside the zip archive.

$zip = new ZipArchive;
$zip->open($source);
$test = $zip->getFromName('index.php');
if(!$test) {
    die('bye bye');
} else {
    die($test开发者_运维问答);
}


Well, the first thing you should do is ensure that you've opened it okay since that can fail as well:

$zip = new ZipArchive;
$rc = $zip->open($source);
if ($rc === TRUE) {
    $test = $zip->getFromName('index.php');
    $zip->close();
    if(!$test) {
        die('bye bye');
    } else {
        die($test);
    }
} else {
    die("could not open: " . $rc);
}

Other than that, make sure you are absolutely certain that your file specification is correct. If necessary, you can use getNameIndex to enumerate the entries one at a time, printing out their names in the process, something like:

$zippy = new ZipArchive();
$zippy->open($source);
for ($i = 0; $i < $zippy->numFiles; $i++) {
    echo $zippy->getNameIndex($i) . '<br />';
}
$zippy->close();

And I'm assuming that I would be wasting my time telling you to check the value of $source. You may want to check, just in case.


As at PHP 8 this can also occur if you are busy building an archive and you try and use getFromName() on a valid file in the archive. I got false no matter what I tried.

eg.

1 create archive
2 ...add things
3 archive->getFromName("valid/archive/file/path/and/name")

Step 3 returns false every time.

However

1 create archive
2 ...add things
A close archive
B re-open archive
3 archive->getFromName("valid/archive/file/path/and/name") 

Step 3 now returns the contents of the file as expected.

So inserting steps A and B made getFromName() work as expected.

Hope this helps someone not waste as much time as I just did :(

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜