Streams is a powerful tool provided by PHP. We often use it inadvertently. If we make good use of it, it will greatly improve the productivity of PHP. . Harnessing the power of Streams will take your applications to the next level.
The following is a description of Streams in the PHP manual:
Streams was introduced in PHP version 4.3.0. It is used to unify the operation methods of files, networks, data compression and other types of files, and provides a set of common function interfaces for these file operations. In short, a stream is a resource object with streaming behavior. In other words, we can read and write to the stream in a linear manner. And you can use fseek() to jump to any position within the stream.
Each Streams object has a wrapper class, in which relevant code for handling special protocols and encodings can be added. Some commonly used packaging classes have been built into PHP, and we can also create and register custom packaging classes. We can even modify and enhance the wrapper class using existing context and filters.
Stream Basics
Stream can be referenced through
PHP’s default packaging class is file://, which means that when we access the file system, we are actually using a stream. We can read the contents of the file in the following two ways, readfile('/path/to/somefile.txt') or readfile('file:///path/to/somefile.txt'). The methods are equivalent. If you use readfile('http://google.com/'), then PHP will select the HTTP stream wrapper class to operate.
As mentioned above, PHP provides many built-in wrapper classes, protocols and filters. According to the method described below, you can query the packaging classes supported by this machine:
print_r(stream_get_transports());
print_r(stream_get_wrappers());
print_r(stream_get_filters());
The output on my machine is:
Array
(
[0] => tcp
[1] => udp
[2] => unix
[3] => udg
[4] => ssl
[5] => sslv3
[6] => sslv2
[7] => tls
)
Array
(
[0] => https
[1] => ftps
[2] => compress.zlib
[3] => compress.bzip2
[4] => php
[5] => file
[6] => glob
[7] => data
[8] => http
[9] => ftp
[10] => zip
[11] => phar
)
Array
(
[0] => zlib.*
[1] => bzip2.*
[2] => convert.iconv.*
[3] => string.rot13
[4] => string.toupper
[5] => string.tolower
[6] => string.strip_tags
[7] => convert.*
[8] => consumed
[9] => dechunk
[10] => mcrypt.*
[11] => mdecrypt.*
)
There are a lot of functions provided, doesn’t it look good?