PHPStreams (stream) details and usage. PHPStreams (stream) Detailed introduction and use this article mainly introduces PHPStreams (stream) Detailed introduction and use, PHPStreams is a built-in core operation, may be rarely used by General developers, it is used to unify PHP Streams (stream) Detailed introduction and use
This article mainly introduces the detailed introduction and use of PHP Streams (stream). PHP Streams is a built-in core operation, which may be rarely used by developers, it is used to unify file, network, data compression, and other file operations, and provides a set of common function interfaces for these file operations. For more information, see
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 use :// . Where Is the name of the packaging class, The content in is specified by the syntax of the packaging class, and the syntax of different packaging classes will be different.
Let's take a look at the built-in packaging classes in PHP by default:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* 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:
?
1 2 3 4 5 6 7 8 |
$ Opts = array ( 'Http' => array ( 'Method' => "GET ", 'Timeout' => 60, ) ); $ Context = stream_context_create ($ opts ); $ Html = file_get_contents ('http: // www.jb51.net', false, $ context ); |
(2) for filter filters, first let's look at the built-in filters in PHP:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* 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: LINE1 WORD-2 Easy as 123 */ |
The streams function list in PHP is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Stream_bucket_append function: adds data to a queue. Stream_bucket_make_writeable function: returns a data object from the operation queue. Stream_bucket_new function: creates a new data for the current queue. Stream_bucket_prepend function: Prepare Data to the queue Stream_context_create function: create a data stream context Stream_context_get_default function: obtains the default data stream context. Stream_context_get_options function: gets the data stream settings. Stream_context_set_option: Set the data stream, data packet, or context. Stream_context_set_params function: sets parameters for data streams, data packets, or context. Stream_copy_to_stream function: replicate data streams. Stream_filter_append function: Adds a filter to the data stream. Stream_filter_prepend function: Adds a filter to the data stream. Stream_filter_register function: registers a data stream filter and runs it as a PHP class. Stream_filter_remove function: removes a filter from a data stream. Stream_get_contents function: read the remaining data from the data stream to the string Stream_get_filters function: returns the list of registered data stream filters. Stream_get_line function: obtains rows from data stream resources based on the given delimiters. Stream_get_meta_data function: obtains the header/metadata from the Encapsulation Protocol file pointer. Stream_get_transports function: return the registered Socket transport list Stream_get_wrappers function: returns the list of registered data streams. Stream_register_wrapper function: registers a URL Encapsulation Protocol implemented using the PHP class. Stream_select function: receives data stream arrays and waits for their status to change. Stream_set_blocking function: sets a data stream to congested or non-congested. Stream_set_timeout function: sets the time-out of data streams. Stream_set_write_buffer function: sets a buffer for the data stream. Stream_socket_accept function: accepts the Socket connection created by the function stream _ socket_server (). Stream_socket_client function: Opens the Socket connection of the network or UNIX host Stream_socket_enable_crypto function: Enables or disables data encryption for a connected Socket. Stream_socket_get_name function: get the local or network Socket name Stream_socket_pair function: creates two non-differentiated Socket data stream connections. Stream_socket_recvfrom function: obtains data from a Socket, regardless of whether it is connected or not. Stream_socket_sendto function: sends data to the Socket, regardless of whether it is 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: deregister a URL package |
Http://www.bkjia.com/PHPjc/1000117.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1000117.htmlTechArticlePHP Streams (stream) Detailed introduction and use this article mainly introduces the PHP Streams (stream) Detailed introduction and use, PHP Streams is a built-in core operation, may be rarely used by General developers, it is used to unify...