bzopen
(PHP 4 >= 4.0.4, PHP 5)
bzopen — Opens a bzip2 compressed file
Description
resource bzopen
( string
$filename
, string $mode
)bzopen() opens a bzip2 (.bz2) file for reading or writing.
Parameters
-
filename
-
The name of the file to open, or an existing stream resource.
-
mode
-
Similar to the fopen() function, only 'r' (read) and 'w' (write) are supported. Everything else will cause bzopen to return
FALSE
.
Return Values
If the open fails, bzopen() returns FALSE
, otherwise
it returns a pointer to the newly opened file.
Examples
Example #1 bzopen() example
<?php
$file = "/tmp/foo.bz2";
$bz = bzopen($file, "r") or die("Couldn't open $file for reading");
bzclose($bz);
?>
Коментарии
Warning!
the example show above is _not_ working in every case!
This example will continue reading until there is no more data:
<?PHP
$bz=bzopen('foo.bz2', 'r');
$data="";
do {
$line=bzread($bz, 8092);
if($line!==false)
$data.=$line;
}
while($line);
bzclose($bz);
?>
In some circumstances, you may want to send a bzip2 stream to the client.
To do this, you need only do:
<?php
ob_flush();
$bz = bzopen('php://stdout', 'w');
bzwrite($bz, 'some input here');
bzclose($bz);
?>
However, please note, because you are using STDOUT, you need to ob_flush() before actually writing to the stream. Otherwise, you might be sending data before the headers, which will cause errors on both server and client ends, in most cases.
You might be able to use php://output rather than php://stdout, however in my tests (with Linux), php://output doesn't actually work - at all.