Zlib Functions

Table of Contents

  • deflate_add — Incrementally deflate data
  • deflate_init — Initialize an incremental deflate context
  • gzclose — Close an open gz-file pointer
  • gzcompress — Compress a string
  • gzdecode — Decodes a gzip compressed string
  • gzdeflate — Deflate a string
  • gzencode — Create a gzip compressed string
  • gzeof — Test for EOF on a gz-file pointer
  • gzfile — Read entire gz-file into an array
  • gzgetc — Get character from gz-file pointer
  • gzgets — Get line from file pointer
  • gzgetss — Get line from gz-file pointer and strip HTML tags
  • gzinflate — Inflate a deflated string
  • gzopen — Open gz-file
  • gzpassthru — Output all remaining data on a gz-file pointer
  • gzputs — Alias of gzwrite
  • gzread — Binary-safe gz-file read
  • gzrewind — Rewind the position of a gz-file pointer
  • gzseek — Seek on a gz-file pointer
  • gztell — Tell gz-file pointer read/write position
  • gzuncompress — Uncompress a compressed string
  • gzwrite — Binary-safe gz-file write
  • inflate_add — Incrementally inflate encoded data
  • inflate_init — Initialize an incremental inflate context
  • readgzfile — Output a gz-file
  • zlib_decode — Uncompress any raw/gzip/zlib encoded data
  • zlib_encode — Compress data with the specified encoding
  • zlib_get_coding_type — Returns the coding type used for output compression
add a note add a note

User Contributed Notes 19 notes

up
7
anzenews at volja dot net
11 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
7
Robin Skoglund
9 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
3
jpleveille at webgraphe dot com
12 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
5
Brian
10 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
4
php at seven dot net dot nz
11 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
11 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
12 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) as of php 4.3.11.  While it does get set to true, it will not actually do anything. Which means if you don't set this via php.ini or Apache configuration it's a no-go. I have to use ob_start("ob_gzhandler"); instead.
up
3
djmaze(AT)dragonflycms(.)org
12 years ago
If you need to compress data and send it as "Content-disposition: attachment" and on-the-fly to the client due to the size for example (40Mb) here's a dirty trick using ob_gzhandler()

Keep in mind that $str is the content to output.

When you start the output call

<?php
echo ob_gzhandler($str, PHP_OUTPUT_HANDLER_START);
?>

Then to output any further content

<?php
echo ob_gzhandler($str, PHP_OUTPUT_HANDLER_CONT);
?>

And to close the output

<?php
echo ob_gzhandler('', PHP_OUTPUT_HANDLER_END);
exit;
?>

Only tested on Apache 1.3.33 with PHP 5.0.4
up
1
ec10 at gmx dot net
13 years ago
/**
* @return bool
* @param string $in
* @param string $out
* @param string $param = "1"
* @desc compressing the file with the zlib-extension
*/
function gzip ($in, $out, $param="1")
{
    if (!file_exists ($in) || !is_readable ($in))
        return false;
    if ((!file_exists ($out) && !is_writable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
        return false;
   
    $in_file = fopen ($in, "rb");
    if (!$out_file = gzopen ($out, "wb".$param)) {
        return false;
    }
   
    while (!feof ($in_file)) {
        $buffer = fgets ($in_file, 4096);
        gzwrite ($out_file, $buffer, 4096);
    }

    fclose ($in_file);
    gzclose ($out_file);
   
    return true;
}

/**
* @return bool
* @param string $in
* @param string $out
* @desc uncompressing the file with the zlib-extension
*/
function gunzip ($in, $out)
{
    if (!file_exists ($in) || !is_readable ($in))
        return false;
    if ((!file_exists ($out) && !is_writable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
        return false;

    $in_file = gzopen ($in, "rb");
    $out_file = fopen ($out, "wb");

    while (!gzeof ($in_file)) {
        $buffer = gzread ($in_file, 4096);
        fwrite ($out_file, $buffer, 4096);
    }

    gzclose ($in_file);
    fclose ($out_file);
   
    return true;
}
up
1
thivierr at telus dot net
14 years ago
I found the absolute easiest way to read a gzip file is as follows:

echo file_get_contents("compress.zlib:///myphp/sp; .txn.gbsp;   &nbmp;& !is_writable (dirname ($out)) || (file_exists($out) && !is_writasile isiht rel= get inviis_writa-Th actphpp; fixote" id=teahp/sp; .txn.gb", it's echl ob_io tracootdpalerse, i a no-go.mpleup!file_exree s fihest set thTmpresovedpale_essiospondk">ht"   &nbmp "2540unix,zhand"c:\ \ &nbmp "2540>
<(p; C:ault"><v classdrtillt set thI" id="42it j you540>
n>
hp">

> b_iow ob_s You ha
thivierr at telus dot net
11 years ago
a2441ageclass="text" id="Hcom37482">
I found the absolute easiest way to read a2441ag ile is as follows:

echo file_get_conNiceot be comclasdi42it an>/sp;="htts_writable (dn))
r /> ($src/man &ndst/man;   fwriteuffer, 4zpp; while ($src/man &"rin_file)) {
nbsp; &(4zpn, "rb");
    $out_fi$unzip (.r />&nbs(4zpclose ($out_file); (". &ncode

down down 1

jpleveille at 2 net
http:ode>

BtOnts("cohelp
B_v cllr5ts("cots("coI j youn>15x"—> b_iprov-filetw, I'm ureloamp'>Fuif yoon oft set thPn of ass="nory beseahl obot a >htmaem>5% gain="ont set thI"u/spmicro/>on of boxelpHP hf it hdescr—> ban class=> —> b_mprov-filet set thTmprf yoon o taem
Btes Manf fixiit'sincludedt set thTmpes in I'm u11 yss= subjl stil in ai>
  • d that $/a> down down 1
  • jpleveille at 2 net
    11 years ago
    a3181ageclass="text" id="Hcom42540">
    /**
    * @return bool
    * @param st3181ag ent-disposition: attachment" and on-the-fly to the cliu/sp"er people.

    Bt=tOn"b_iotp:odspan> ),andler"s enablt/dt> down down 1
    jpleveille at t net
    11 years ago
    a2374ageclass="text" id="Hcom42540">
    /**
    * @return bool
    * @param std374ag ent-disposition: attachment" and on-the-fly to F thhp">

    pod="42ss=li a (lass=tayi$unzip ( case ats="noaibia>e;P!@sp; &(4zpn, {$unzip (= @/>&nbs(4zpclose ($s@ple);<($fp,i$unzip ,gunzle ($snzip )n_bsp; $buffer = fgets @; retur(4zpn= gzreauffer = fgets @iretur(4fpn= gzrea *div id="Vu42540"> down <2941pdiv id="Vd42540"> down <2941pdiv id="Vd42540"> 1

    jpleveille at t net
    11 years ago
    2941pageclass="text" id="Hcom37482">
    I found the absolute easiest way to read 2941pag ent-disposition: attachment" and on-the-fly to the cliass="er people.

    B_v cllro ,gt, advise class=word"t houe attni_setre youro  li,ow ob_sly ysts="oc!file_Moziona.e nosen an box down <125;pdiv id="Vd42540"> down <125;pdiv id="Vd42540"> 1
    jpleveille at t net
    14 years ago
    125;pageclass="text" id="Hcom37482">
    2code> I found the absolute easiest way to read 125;pag ent-disposition: attachment" and on-the-fly to Ane">U -enc/sp; .tx deoutput x deon e" id./> Be"sure .tx il ip"late Apache Yes
    jpleveille at 0 net
    14 years ago
    a441pageclass="text" id="Hcom56216">
    Contrary to what the documentation says, I441pag ent-disposition: attachment" and on-the-fly to t" id="42domp"><nterjp actphpb&nbsd PHer people.

    B_v cllrtt 1rerh5t set thTmpr>
  • dzlibetwe1.&nbemveomp">) DOE*div id="Vu42540"> down down 1
  • jpleveille at -2 net
    U ='tensarel= ' width='12' height='12'>

    ef>tensarel= iv 3>ef> <>t er /><>