http://www.php.net/manual/en/intro.stream.php
Streams were introduced with PHP 4.3.0 as a way of generalizing file, network, data compression, and other operations which share a common set of functions and uses. In its simplest definition, a stream is a resource object which exhibits streamable behavior. That is, it can be read from or written to in a linear fashion, and may be able to fseek() to an arbitrary locations within the stream.
Imagine writing to and reading from a file. Now imagine writing to and reading from some non-file resource in exactly the same way. Streams abstract away the details of what you're operating on. It could be a file, a network, a command line program, or others. But as far as the stream functions are concerned, these are all the same thing -- a writable, readable resource.
I used the stream functions once when I wrote code to minify on the fly. I used streams to write input to and read output from the Closure Compiler.
PHP Code:
function compress($jsContent)
{
// Open process
$cmd = 'java -jar compiler.jar';
$descriptors = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
$process = proc_open($cmd, $descriptors, $pipes);
if ($process === false) {
die('proc_open failed');
}
// Write raw
fwrite($pipes[0], $jsContent);
fclose($pipes[0]);
// Read compressed
$compressed = stream_get_contents($pipes[1]);
fclose($pipes[1]);
// Check errors
$errors = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$exitStatus = proc_close($process);
if ($errors != '' or $exitStatus != 0) {
die('errors or exit status failed');
}
return $compressed;
}
Bookmarks