I would like to download a zip archive and unzip it in memory using PHP.
This is what I have today (and it’s just too much file-handling for me 🙂 ):
// download the data file from the real page
copy("http://www.curriculummagic.com/AdvancedBalloons.kmz", "./data/zip.kmz");
// unzip it
$zip = new ZipArchive;
$res = $zip->open('./data/zip.kmz');
if ($res === TRUE) {
$zip->extractTo('./data');
$zip->close();
}
// use the unzipped files...
Warning: This cannot be done in memory —
ZipArchivecannot work with “memory mapped files”.You can obtain the data of a file inside a zip-file into a variable (memory) with
file_get_contentsDocs as it supports thezip://Stream wrapper Docs:You can only access local files with
zip://or via ZipArchive. For that you can first copy the contents to a temporary file and work with it: