abel
abel

Reputation: 2383

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 question will most probably be how do I unzip contents without unzipping them to disk first, but let's see.

Upvotes: 2

Views: 1661

Answers (3)

abel
abel

Reputation: 2383

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

Upvotes: 1

Robert Ros
Robert Ros

Reputation: 1538

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: https://www.php.net/manual/en/function.ziparchive-addfromstring.php

Upvotes: 5

Mike
Mike

Reputation: 21659

You could use the MySQL COMPRESS and UNCOMPRESS functions when you store to, and retrieve from, the database.

Upvotes: 1

Related Questions