Streams are resources provided by PHP that we often use transparently, but which can also be very powerful tools. By learning how to harness their power, we can take our applications to a higher level.
The PHP manual has a great description of streams:
“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.” (PHP Manual)
A stream is referenced as
<scheme> is the name of the wrapper, and
<target> will vary depending on the wrapper’s syntax.
The default wrapper is
file:// which means we use a stream every time we access the filesystem. We can either write
readfile('/path/to/somefile.txt') for example or
readfile('file:///path/to/somefile.txt') and obtain the same result. If we instead use
readfile('http://google.com/') then we’re telling PHP to use the HTTP stream wrapper.
As I said before, PHP provides some built-in wrappers, protocols, and filters. To know which wrappers are installed on our machine we can use:
<?php print_r(stream_get_transports()); print_r(stream_get_wrappers()); print_r(stream_get_filters());
My installation outputs the following:
Array (  => tcp  => udp  => unix  => udg  => ssl  => sslv3  => sslv2  => tls ) Array (  => https  => ftps  => compress.zlib  => compress.bzip2  => php  => file  => glob  => data  => http  => ftp  => zip  => phar ) Array (  => zlib.*  => bzip2.*  => convert.iconv.*  => string.rot13  => string.toupper  => string.tolower  => string.strip_tags  => convert.*  => consumed  => dechunk  => mcrypt.*  => mdecrypt.* )