definition The purpose of streams is to use a unified way to handle operations such as files, networks, and data compression that share the same set of functions and usage. In simple terms, a stream is a resource object with streaming behavior. Therefore, a stream can be read and written linearly, and may also use the fseek() function to locate any position in the stream-PHP manual. Let's simplify it for easy understanding. The purpose of a stream is to transfer data between a source and a destination. The source and destination can be files, command-line processes, network connections, ZIP or TAR archives, temporary memory, standard input or output, or any other resource implemented through the PHP stream encapsulation protocol.
Stream Encapsulation Protocol There are different types of streaming data, and each type requires a unique protocol to read and write data. We call these protocols stream encapsulation protocols. The purpose of stream encapsulation protocols is to encapsulate the differences between different communication methods using a common interface. Each stream has a protocol and a destination. The format is as follows:
Where <scheme> is the encapsulation protocol of the stream, <target> Example: Using HTTP Stream Encapsulation to communicate with the Flickr API
The string argument to the file_get_contents() function is actually a stream identifier. The http protocol tells PHP to use the HTTP stream wrapper. In this argument, after http is the stream target. The stream target looks like a normal web URL because the HTTP stream wrapper specifies that. Other stream wrappers may not be like this. (A normal URL is actually a PHP stream wrapper identifier in disguise). file://stream encapsulation protocol We use file_get_contents(), fopen(), fwrite(), and fclose() to read and write to the file system, and because PHP uses file:// as the default stream wrapper, we rarely think of these functions as using PHP streams. We use PHP streams without even realizing it! Example: Implicit use of file:// stream wrapper
The following example does the same thing, but this time we explicitly specify the stream file:// stream encapsulation protocol in the stream identifier: Example: Explicitly using the file:// stream wrapper protocol
We usually omit the file:// wrapper since this is the default used by PHP. php://stream encapsulation protocol This stream encapsulation protocol is used to communicate with the standard input, standard output, and standard error file descriptors of the PHP script. We can use the file system functions provided by PHP to open, read, or write the following four streams:
This is a read-only PHP stream where data comes from standard input. For example, a PHP script can use this stream to receive information passed to the script on the command line.
The purpose of this PHP stream is to write data to the current output buffer. This stream can only be written, not read or addressed.
The purpose of this PHP stream is to read data from system memory or write data to system memory. The disadvantage of this PHP stream is that the available memory is limited, and it is safer to use the php://temp stream.
This PHP stream works similarly to php://memory, however, when there is no available memory, PHP will write the data to a temporary file. Other stream encapsulation protocols PHP and PHP extensions also provide many other stream encapsulation protocols, for example, for communicating with ZIP and TAR archives, FTP servers, data compression libraries, etc. Flow context Some PHP streams can accept a series of optional parameters, called stream context, which are used to customize the behavior of the stream. The stream context is created using the stream_context_create() function. The context object returned by this function can be passed to most file system and stream functions. Example: Stream context (sending an HTTP POST request using the file_get_contents() function)
Stream Filters The real power of PHP lies in filtering, transforming, adding or removing data transmitted in the stream. |
1. “Winner-takes-all” and multi-sided platforms w...
We have shared edgeNAT several times in the tribe...
"Are you planning to transfer your number to...
Most IT organizations are under pressure to be mo...
What problems does each generation of HTTP solve?...
As of the end of October 2016, the number of Chin...
In 2019, we often heard the industry say that 201...
DiyVM is a Chinese hosting company that has been ...
The background and significance of data empowerin...
On December 4, Wang Zhen, Vice President of R&...
[[181724]] Some people say that 4G has changed ou...
My wife, who has always been worried about her ch...
DediPath has released its latest summer promotion...
WePC uses the .au domain name and describes itsel...
A lot can change in life in a year, and for the 5...