Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

<?
// open compressed file and print each line
if($myFile = gzopen("data.gz", "r"))
{
while(!gzeof($myFile))
{
$myLine = gzgetss($myFile, 255);
print($myLine);
}


gzclose($myFile);
}
?>


integer gzopen(string filename, string mode, boolean
use_include_path)


The gzopen function is similar in operation to the fopen function, except that it operates
on compressed files. If the use_include_path argument is TRUE, the include path
specified in php.ini will be searched. See gzgets and gzputs for examples of use.


The mode argument accepts a few extra parameters compared to fopen. In addition to the
modes listed in Table 8.6, you may specify a compression level and a compression
strategy if you are creating a new file. Immediately following the write mode, you may
place an integer between zero and nine that specifies the level of compression. Zero
means no compression, and nine is maximum compression. After the compression level,
you may use h to force Huffman encoding only, or f to optimize for filtered input.
Filtered data is defined by the zlib source code as being small values of somewhat
random distribution. In almost all cases the default settings are a good choice and the
extra mode settings are unnecessary.


It is possible to open an uncompressed file with gzopen. Reads from the file will operate
as expected. This can be convenient if you do not know ahead of time whether a file is
compressed.


boolean gzpassthru(integer file_handle)


The gzpassthru function prints the contents of the compressed file to the browser,
exactly like the fpassthru function.


<?
// open a compressed file
if(!($myFile = gzopen("data.html.gz", "r")))
{
print("file could not be opened");
exit;
}

Free download pdf