Had some trouble finding the correct way to send a Content-Length header with HTTP compression. The pitch is to use gzencode (not gzdeflaten not gzcompress).
// disable ZLIB ouput compression ini_set('zlib.output_compression','Off');
// compress data $gzipoutput = gzencode($output,6);
// various headers, those with # are mandatory header('Content-Type: application/x-download'); header('Content-Encoding: gzip'); # header('Content-Length: '.strlen($gzipoutput)); # header('Content-Disposition: attachment; filename="myfile.name"'); header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); header('Pragma: no-cache');
// output data echo $gzipoutput;
function _compress( $data ) {
$supportsGzip = strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;
if ( $supportsGzip ) { $content = gzencode( trim( preg_replace( '/\s+/', ' ', $data ) ), 9); } else { $content = $data; }
$offset = 60 * 60; $expire = "expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header('Content-Encoding: gzip'); header("content-type: text/html; charset: UTF-8"); header("cache-control: must-revalidate"); header( $expire ); header( 'Content-Length: ' . strlen( $content ) ); header('Vary: Accept-Encoding');
echo $content;
}
_compress( "Some test data" );
if (isset($headers['Content-Encoding']) && ($headers['Content-Encoding'] === 'gzip' || $headers['Content-Encoding'] === 'deflate')) { if ($headers['Content-Encoding'] === 'gzip') { $buffer = substr($buffer, 10); } $contents = @gzinflate($buffer); if ($contents === false) { return false; } }
|