PHPStreams (stream) details and _ php instances

Source: Internet
Author: User
This article mainly introduces PHPStreams (stream) details and usage. PHPStreams 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, if you need it, you can refer to PHP Streams as a built-in core operation, which is rarely used by General 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:

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.jb51.net', 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.