note 21980 added to function.gzcompress
| From: | postmaster at ak-dev dot com | Date: | Sun, 02 Jun 2002 20:57:19 +0000 |
| Subject: | note 21980 added to function.gzcompress | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31351@lists.php.net to get a copy of this message | ||
Second try! In my mind, the following source code is right with 4.1.2 and higher, and seems to be
right with other versions... and its works with all browser I tried(NS 4.7x, mozilla 1.00rc3, IE5.5,
opera6.01)...
As it is said in documentation of the function gzencode, you have to use this function, if you want
to get the right result! gzcompress doesn't create the right stream output for browsers!(It is
explained in gzencode documentation: generated headers are not the same!)
The source code:
<?
function checkCanGzip() {
global $HTTP_ACCEPT_ENCODING;
if (headers_sent()) return 0;
if (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false) return
"x-gzip";
if (strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false) return
"gzip";
return 0;
}
function gzDocOut() {
if ($encoding = checkCanGzip()) {
$contents = ob_get_contents();
ob_end_clean();
header("Content-Encoding: ".$encoding);
// print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$contents = gzencode($contents);
// $contents = substr($contents, 0, strlen($contents) - 4);
print($contents);
// print(pack('V', crc32($contents)));
// print(pack('V', strlen($contents)));
exit();
}
else {
ob_end_flush();
exit();
}
}
ob_start();
ob_implicit_flush(0);
print("your stuff...");
gzDocOut();
?>
You have to note, that gzencode is able to generate deflate compression, but the previous source
code, does not implemente it. You just have to read gzencode documentation to find this tip.
function gzencode in php 4.2.0 (and higher) allows to use a compression level, it is not possible in
loder versions.
The code which is commented, is wrong source code, used, in the different exemple found on this
page!
--
http://www.php.net/manual/en/function.gzcompress.php
http://master.php.net/manage/user-notes.php?action=edit+21980
http://master.php.net/manage/user-notes.php?action=delete+21980
http://master.php.net/manage/user-notes.php?action=reject+21980