I have some textstrings in memory and want to create a simple ZIP from it. Do I have to create a textfile and store it on the server first or can I create a ZIP directly from memory and store it finally. This would save time and action.
3
-
2php.net/manual/en/ziparchive.addfromstring.phpDevon Bessemer– Devon Bessemer2016-10-10 12:53:00 +00:00Commented Oct 10, 2016 at 12:53
-
6I am pretty sure that manual is not closed today... php.net/manual/en/ziparchive.addfromstring.phpnospor– nospor2016-10-10 12:53:01 +00:00Commented Oct 10, 2016 at 12:53
-
Hi guys - I was reading the manual. Obviously must have overread this doe to a lot of ZIP SO answers relating to add files from disk. Thanks to people downvoting, very tolerant !Ben– Ben2016-10-10 12:58:41 +00:00Commented Oct 10, 2016 at 12:58
Add a comment
|
1 Answer
You can use addFromString() from ZipArchive.
Example very much alike the one from manual:
<?php
$content = 'My content in Memory';
$zip = new ZipArchive;
$res = $zip->open('archive.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFromString('file.txt', $content);
$zip->close();
} else {
echo 'Error: Zip couldn\'t be created.';
}
?>