Zip HTML/PDF, store as Blob
I have a variable which contains the html content, $html
and a variable with the pdf content $pdf
.
I can create zip files by using $zip->addFile($file,$file);
where $file is a file on the disk
However, I want to create zip files with content from the variables, without having to write them to disk first. How do I do it?
I am already storing $html and $pdf as-is in the db. Zipping them would help me save some space in my db.
My next q开发者_JAVA技巧uestion will most probably be how do I unzip contents without unzipping them to disk first, but let's see.
From $zip->addFile($file,$file);
I guess you're using the ZipArchive PHP extension. You can create files in the Zip archive by using the method addFromString()
$zip->addFromString('file.html', $html_data);
$zip->addFromString('file.pdf', $pdf_data);
Also see: http://nl3.php.net/manual/en/function.ziparchive-addfromstring.php
You could use the MySQL COMPRESS
and UNCOMPRESS
functions when you store to, and retrieve from, the database.
Since I am using php, I found this to be the best solution.
$html="<h1>Contains HTML</h1>";
$ziphtml=gzcompress($html,9);
$statement=mysql_query("INSERT into TABLE (field) VALUES ('$ziphtml')",$db);
and then uncompress when you read the field using gzuncompress
http://php.net/manual/en/function.gzcompress.php
精彩评论