Search code examples
phpzipphp-ziparchive

zip archive not opening


So I have the following code:

$z = new ZipArchive();
$zopen = $z->open('https://github.com/AdamKyle/Aisis-Framework/archive/Aisis1.1.7.zip', 4);
if ($zopen === true) {
    for ($i = 0; $i < $zip->numFiles; $i++) {
        $filename = $zip->getNameIndex($i);
        var_dump($filename);
    }
} else {
    echo "fail";
}
exit;

And it keeps resulting in failure. The URL I am providing works for me in the browser, but not in the above code. What's going on?

Update

I was asked what the resaults of:

var_dump(stream_get_wrappers());

were and they are bellow

array (size=12)
  0 => string 'https' (length=5)
  1 => string 'ftps' (length=4)
  2 => string 'compress.zlib' (length=13)
  3 => string 'compress.bzip2' (length=14)
  4 => string 'php' (length=3)
  5 => string 'file' (length=4)
  6 => string 'glob' (length=4)
  7 => string 'data' (length=4)
  8 => string 'http' (length=4)
  9 => string 'ftp' (length=3)
  10 => string 'phar' (length=4)
  11 => string 'zip' (length=3)

Solution

  • After having a read I believe ZipArchive does not support opening zip files via a URI.

    In order to work with the zip, you would first need to copy it to your local disk...

    $content = file_get_contents('https://github.com/AdamKyle/Aisis-Framework/archive/Aisis1.1.7.zip');
    file_put_contents('/tmp/name.zip', $content);
    $zip = new ZipArchive();
    $zip->open('/tmp/name.zip');
    

    Make sure that the allow_url_fopen ini setting is enabled.