PHP Streams (Stream) detailed introduction and use, phpstreams
PHP Streams is a built-in core operation that is rarely used by developers. It is used for file operations such as unified file, network, and data compression, provides a set of common function interfaces for these file operations.
A stream is a resource object with streaming behaviors. Each stream object has a packaging class. Stream can be referenced by <scheme >:/// <target>. <Scheme> is the name of the packaging class, and the content in <target> is specified by the packaging class syntax. the syntax of different packaging classes varies.
Let's take a look at the built-in packaging classes in PHP by default:
print_r(stream_get_wrappers());/*Array( [0] => php [1] => file [2] => glob [3] => data [4] => http [5] => ftp [6] => zip [7] => compress.zlib [8] => https [9] => ftps [10] => phar)*/
See the protocols and packaging classes supported by PHP in the PHP manual.
The following code uses file_get_contents () to obtain data:
/* Read local file from /home/bar */ $localfile = file_get_contents ( "/home/bar/foo.txt" ); /* Identical to above, explicitly naming FILE scheme */ $localfile = file_get_contents ( "file:///home/bar/foo.txt" ); /* Read remote file from www.example.com using HTTP */ $httpfile = file_get_contents ( "http://www.example.com/foo.txt" ); /* Read remote file from www.example.com using HTTPS */ $httpsfile = file_get_contents ( "https://www.example.com/foo.txt" ); /* Read remote file from ftp.example.com using FTP */ $ftpfile = file_get_contents ( "ftp://user:pass@ftp.example.com/foo.txt" ); /* Read remote file from ftp.example.com using FTPS */ $ftpsfile = file_get_contents ( "ftps://user:pass@ftp.example.com/foo.txt" );
In fact, readfile ('/path/to/somefile.txt') or readfile ('file: // path/to/somefile.txt ') are equivalent. Because the default PHP packaging class is file ://.
The manual clearly states that you can use stream_register_wrapper () to register your own package. You can refer to the examples in the manual.
OK. Here is a brief introduction to PHP: //, which is a packaging class for PHP to process IO streams (Click here to see an example ). PHP: // you can access more powerful input/output streams:
Php: // stdin: access the input stream of the PHP process, for example, the keyboard input used to obtain the cli script.
Php: // stdout: access the corresponding output stream of the PHP process.
Php: // stderr: Error output for accessing the PHP process.
Php: // input: Read-Only stream of the requested raw data.
Php: // output: Write-only data streams, which are written to the output zone in the same way as print and echo.
Php: // fd: allows direct access to the specified file descriptor. For example, php: // fd/3 references file descriptor 3.
Php: // memory: allows reading and writing temporary data. Store data in memory.
Php: // temp: Same as above. It will be stored in temporary files after the internal storage reaches the predefined limit (2 MB by default.
Php: // filter: filter.
PHP can also modify and enhance the packaging class through context and filter.
(1) regarding context, for example, PHP uses stream_context_create () to set the time-out period for obtaining files. This code must have been used:
$opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>60, ));$context = stream_context_create($opts);$html =file_get_contents('http://www.bkjia.com', false, $context);
(2) For filter filters, first let's look at the built-in filters in PHP:
print_r(stream_get_filters());/*Array( [0] => convert.iconv.* [1] => mcrypt.* [2] => mdecrypt.* [3] => string.rot13 [4] => string.toupper [5] => string.tolower [6] => string.strip_tags [7] => convert.* [8] => consumed [9] => dechunk [10] => zlib.*)*/
You can use stream_filter_register () and the built-in php_user_filter to create a custom filter, as shown below:
/* Define our filter class */class strtoupper_filter extends php_user_filter {function filter ($ in, $ out, & $ consumed, $ closing) {while ($ bucket = stream_bucket_make_writeable ($ in) {$ bucket-> data = strtoupper ($ bucket-> data); $ consumed + = $ bucket-> datalen; stream_bucket_append ($ out, $ bucket);} return PSFS_PASS_ON;}/* Register our filter with PHP */stream_filter_register ("strtoupper", "strtoupper_filter ") or die ("Failed to register filter"); $ fp = fopen ("foo-bar.txt", "w "); /* Attach the registered filter to the stream just opened */stream_filter_append ($ fp, "strtoupper"); fwrite ($ fp, "Line1 \ n"); fwrite ($ fp, "Word-2 \ n"); fwrite ($ fp, "Easy As 123 \ n"); fclose ($ fp); readfile ("foo-bar.txt "); /* The result is AS follows: LINE1WORD-2 easy as 123 */
The streams function list in PHP is as follows:
Stream_bucket_append function: adds data into the queue function: returns a data object from the Operation queue. stream_bucket_new function: Creates a new data stream_bucket_prepend function for the current queue. functions: create a data stream context stream_context_get_default function: Get the default data stream context stream_context_get_options function: Get the stream_context_set_option function for data stream settings: Set stream_context_set_params function for data stream, data packet, or context: set the stream_copy_to_stream parameter for data streams, data packets, or contexts. function: adds the filter stream_filter_append to the data stream. function: adds the filter stream_filter_prepend to the data stream. stream_filter_register function: register a data stream filter and execute the stream_filter_remove function as a PHP class: remove the filter from a data stream stream_get_contents function: Read the remaining data from the data stream to the string stream_get_filters function: return the registered data stream filter list stream_get_line function: obtains the stream_get_meta_data function from the data stream resource according to the given delimiters: obtains the header/metadata stream_get_transports function from the encapsulation protocol file pointer: return to the registered Socket transport list stream_get_wrappers function: Return to the registered data stream list stream_register_wrapper function: register a URL Encapsulation Protocol stream_select function implemented using the PHP class: the stream_set_blocking function receives data stream arrays and waits for changes in their statuses: sets a data stream as a blocked or non-congested stream_set_timeout function: sets stream_set_write_buffer for data stream timeout. The stream_socket_accept function is set: accept the Socket connection stream_socket_client function created by the function stream _ socket_server (): Open the network or UNIX host Socket connection stream_socket_enable_crypto function: open or close the Data Encryption stream_socket_get_name function for a connected Socket: obtain the local or network Socket name stream_socket_pair function: create two different Socket data streams to connect to the stream_socket_recvfrom function: obtain data from the Socket, whether connected or not stream_socket_sendto function: send data to the Socket, whether connected or not, stream_socket_server function: Creates a network or UNIX Socket server stream_wrapper_restore function: Restores a previously logged-out data packet stream_wrapper_unregister function: cancels a URL address package.