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. |
According to a new report released by Kentik, clo...
Recently, the three major operators have released...
After a year of development, AI technology and ap...
It has been a while since I shared information ab...
[[397802]] This article is reprinted from the WeC...
[[185474]] RS485 bus is widely used in video surv...
The global 5G in IoT market is experiencing signi...
The world is so big, thank you for visiting me!! ...
SD-WAN technology helps make wide area networks m...
[Beijing, China, June 5, 2019] Huawei released it...
As the construction of 5G networks continues to a...
In today's digital era, AI and large model ap...
HXServers is a new foreign hosting company that o...
Public transport Wi-Fi operator 16Wi-Fi recently ...
As of the end of October 2016, the number of Chin...