SunshinePHP 2019

Zlib Functions

Table of Contents

add a note add a note

User Contributed Notes 19 notes

up
7
anzenews at volja dot net
12 years ago
php at seven dot net dot nz:
Have you been using register_shutdown_function() that outputs something? If yes, some of the output was not encoded and IE6 is less than forgiving about that. In my case it was reloading the page and sometimes showing a blank page. So IE6 does get invalid stream, bug is not bogus, only description is not good enough.

jpleveille at webgraphe dot com:
In PHP 4.4.2 register_shutdown_function() DOES output whatever you wish, but it is not encoded with the rest of the document. Which is even worse as you get a bug that is difficult / impossible to track down.
up
3
jpleveille at webgraphe dot com
13 years ago
register_shutdown_function() (http://www.php.net/register_shutdown_function) won't output anything if you use zlib.output_compression.

Shutdown function is called after closing all opened output buffers thus, for example, its output will not be compressed if zlib.output_compression is enabled.
up
3
admin_at_commandline_dot_ch
14 years ago
My gzip function.

This function read, compress and writhe only small chunks at one time, this way you can compress big files without memory problems...

<?php
function gzip($src, $level = 5, $dst = false){
    if(
$dst == false){
       
$dst = $src.".gz";
    }
    if(
file_exists($src)){
       
$filesize = filesize($src);
       
$src_handle = fopen($src, "r");
        if(!
file_exists($dst)){
           
$dst_handle = gzopen($dst, "w$level");
            while(!
feof($src_handle)){
               
$chunk = fread($src_handle, 2048);
               
gzwrite($dst_handle, $chunk);
            }
           
fclose($src_handle);
           
gzclose($dst_handle);
            return
true;
        } else {
           
error_log("$dst already exists");
        }
    } else {
       
error_log("$src doesn't exist");
    }
    return
false;
}
?>
up
5
Brian
11 years ago
If you have zlib.output_compression set to on when your script starts (in php.ini or via apache directive), then you disable it at runtime using ini_set() before producing output, your output will still be buffered!  Even if you call ob_implicit_flush().

If you want unbuffered output, you must disable zlib.output_compression before your script starts (as well as mod_gzip of course, if you have both installed).

This behaviour experienced on Server: Apache/1.3.33 (Debian GNU/Linux) mod_gzip/1.3.26.1a PHP/4.3.10-18
up
6
Robin Skoglund
10 years ago
ini_set('zlib.output_compression', 'On') doesn't work

It says that zlib.output_compression is PHP_INI_ALL changable, but in my case at least, this is not true. When using ini_set('zlib.output_compression', 'On') runtime, phpinfo() states that zlib.output_compression is enabled, yet there is no compression.

Since I don't want to enable zlib.ouput_compression for all sites in php.ini, my solution was to enable it in apache conf (.htaccess should work as well) with the line:
php_flag zlib.output_compression on

Just spent hours figuring this out, so I hope it helps other people.

Btw, I'm using Apache/2.2.4 (Ubuntu) PHP/5.2.3-1ubuntu6.3.
up
4
php at seven dot net dot nz
12 years ago
zlib.output_compression has caused problems for me in IE 6, when pages are sometimes not displayed. This could be difficult to track down and PHP have no intention of fixing it, so hopefully you find this note in a search if it happens to you.

http://bugs.php.net/bug.php?id=38026
up
4
Arne dot Heizmann at csr dot com
12 years ago
PHP Version 5.1.4 here. What Bob said is correct even in this version (newest at the time of writing). You can't enable zlib.output_compression via ini_set(). You have to use php.ini.
up
3
Bob
13 years ago
Contrary to what the documentation says, I've been unable to get zlib.output_compression to work via ini_set() (Even though I put it at the very beginning of the file before any output was sent