Streams in PHP
Article Summary
PHP streams are an asynchronous, multi-threaded implementation of the File System API. Streams provide access to files, networks, data compression, and other resources that exhibit streamable behavior, meaning they can be read from in linear fashion or written to in a single operation. There are a variety of different wrappers or stream contexts that have specific capabilities. Wrappers include the default file system wrapper, HTTP, FTP, and ZIP.
For example, suppose you have a list of email addresses that you want to loop over and process line by line. You might want to add a header to each message, replace all company names with a custom tag, or even set up an email alert for each individual name that appears. For this task, it would be best to use a filtered stream.
A filtered stream is a special type of stream that allows you to filter data as it passes through the stream. Filters are implemented by extending the PHP_User_Filter base class and then registered with PHP via stream_filter_register(). The resulting filter is then appended to a stream when it is created.
When a call to a stream related function fails it is generally because the requested resource could not be accessed. This can be because it is a remote address or the requested file is not available on the local system. In these cases the error will be E_WARNING. In other situations, the stream related function might fail because the necessary wrapper or context is not registered in PHP. See the array returned by stream_get_wrappers() for a list of supported streams in your installation of PHP.