use PHP ZipArchive with file as variable
You can use tmpfile() to create a temporary file, write to it and then use it in the zip. Example:
<?php$zip = new ZipArchive();$zip->open(__DIR__ . '/zipfile.zip', ZipArchive::CREATE);$fp = tmpfile();fwrite($fp, 'Test');$filename = stream_get_meta_data($fp)['uri'];$zip->addFile($filename, 'filename.txt');$zip->close();fclose($fp);
Little improve:
$zipContent; // in this variable could be ZIP, DOCX, XLSX etc.$fp = tmpfile();fwrite($fp, $zipContent);$stream = stream_get_meta_data($fp);$filename = $stream['uri'];$zip = new ZipArchive();$zip->open($filename);// profit!$zip->close();fclose($fp);
Just make no sense to create "zipfile.zip" and add file inside, because we already have a variable.
Check out this answer at https://stackoverflow.com/a/53902626/859837
There is a way to create a file which resides in memory only.