Re: Cannot make bzip2 stream "on the fly"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thank you for response.
Script is server-side. It is to send a packed data as file (but the source isn't actually a file) via HTTP to client. bzcompress() are not suitable, because it cannot pack the stream on the fly (I have to store all amount of data in local variable before call it).
My task is already SOLVED in the different way:
$bz = fopen('php://output', 'w');
$param = array('blocks' => 6, 'work' => 0);
stream_filter_append($bz, 'bzip2.compress', STREAM_FILTER_WRITE, $param);
...writing to $bz using fwrite() as into stream...
fclose($bz);
it gives exactly what I need.
But still I wonder why bzopen() isn't work, meanwhile the phpdoc tells that it should work that way.

---
Maciek Sokolewicz wrote:
On 31-01-2012 15:34, Michael Shestero wrote:

header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=f.bzip2");
header("Content-Type: application/x-bzip2");
//header("Content-length: " . strlen($zippedfile) . "\n\n");
header("Content-Transfer-Encoding: binary");
ob_flush();

$bz = bzopen( 'php://output' , 'w' ); if ($bz===FALSE) { echo "FALSE";
return; }
bzwrite($bz,"hi");
bzclose($bz);

bzopen returns error:
bzopen(): cannot represent a stream of type Output as a File Descriptor

in case using stdout instead of output it works but produce zero result.

Following works ok:

$z = fopen( 'php://output' , 'w' );
if ($z===FALSE) { echo "FALSE"; return; }
fwrite($z,"hihi");
fclose($z);

Please, help!



What exactly are you trying to do? From the top section it seems like you're trying to output back via the standard output channel, meaning as the body of a response. This assumes you're working in a client/server relationship, where PHP is invoked server-side as a script, and its response is being sent back to the client (browser).

But then all of a sudden, you start opening php://output which is an output stream which exists solely in the cli-mode!

So, your answer is simply:
1. in the case of a browser/server type of relation:
<?php
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=f.txt.bz2");
header("Content-Type: application/x-bzip2");
header("Content-Transfer-Encoding: binary");

$compressed_string = bzcompress("hi");

echo $compressed_string;

2. In the case that you're using php-cli, get rid of all the header stuff. It's useless here.

- Tul


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux