Search code examples
phpzipphp-ziparchive

Cannot extract zip file in php, no feedbackor error


I am retrieving my google map in a kmz format like this:

file_put_contents($_SERVER['DOCUMENT_ROOT'].'/temp/map.kmz', file_get_contents('https://mapsengine.google.com/map/kml?mid=zLucZBnh_ipg.kS906psI1W9k') );

$zip = new ZipArchive;
$res = $zip->open($_SERVER['DOCUMENT_ROOT'].'/temp/map.kmz');
if ($res === true)
{
    trace("Number of files: $res->numFiles".PHP_EOL);
    for( $i = 0; $i < $res->numFiles; $i++ )
    { 
        $stat = $res->statIndex( $i ); 
        print_r( basename( $stat['name'] ) . PHP_EOL ); 
    }
}

But no files are showing and $zip->extractTo() is not working either. The file is downloaded on the server and I can extract it manually though. I have tried renaming the file to .zip or .kmz, still not working. I have opened the map.kmz file in Winrar and it does indeed say that it is a zip file format.

Any idea why it's not working? Do I need some special permissions to read the number of files or extract?


Solution

  • Got tired of the damn class not working, tried this method instead and it works:

    $data = file_get_contents("https://mapsengine.google.com/map/kml?mid=zLucZBnh_ipg.kS906psI1W9k");
    file_put_contents($_SERVER['DOCUMENT_ROOT'].'/temp/kmz_temp', $data);
    ob_start();
    passthru("unzip -p {$_SERVER['DOCUMENT_ROOT']}/temp/kmz_temp");
    $xml_data = ob_get_clean();
    header("Content-type: text/xml");
    echo $xml_data;
    exit();