A brief analysis of flow (streams) in PHP

Source: Internet
Author: User
Tags file system http request readfile wrapper wrappers

This article mainly introduces the flow in PHP (streams) analysis, this article explains the flow of the overview, the flow of basic knowledge, php://Wrapper, stream context (stream contexts) and so on, the need for friends can refer to the

Overview

Streaming (streams) is a feature introduced by the PHP4.3 version, primarily for the purpose of unifying the working methods of files, sockets, and other similar resources. PHP4.3 is a long time away, but many programmers don't seem to be able to use the stream in PHP correctly, including me, of course. Before in some programs have encountered the use of the flow, such as php://input, but has not been able to organize, today to put this part of the knowledge to organize.

The stream is a resource provided by PHP that we can use transparently, and the stream is a very powerful tool. Proper use of the flow in the program can bring our program to a new height.

The description of convection in the PHP manual is as follows:

Copy code code as follows:

Streams were introduced with PHP 4.3.0 as a way to generalizing file, network, data compression, and other operations whic H share a common set of functions and uses. In it simplest definition, a stream is a resource object which exhibits streamable behavior. It, it can be read from or written to in a linear fashion, and May are able to fseek () to a arbitrary locations withi n the stream.

Each stream implements a wrapper (wrapper), and the wrapper contains some extra code to handle special protocols and encodings. PHP offers some built-in wrappers, and we can easily create and register custom wrappers. We can even use context (contexts) and filters to change and enhance the wrapper.

Streaming Basics

PHP streaming in the form of: :// . is the name of the wrapper, and the content depends on the different wrapper syntax.

The default wrapper is file://, which means the stream is used every time we access the file system. For example, we can read files in two ways: ReadFile ('/path/to/somefile.txt ') and ReadFile (' File:///path/to/somefile.txt '), reading files in both ways, Can get the same result.

As mentioned earlier, PHP provides some built-in wrappers, protocols, and filters. To see which wrappers are installed on our machines, you can use the following functions:

Copy code code as follows:

  

Var_dump (Stream_get_transports ());

Var_dump (Stream_get_wrappers ());

Var_dump (Stream_get_filters ());

?>

My local environment output is as follows:

Copy code code as follows:

Array (size=8)

0 => string ' tcp ' (length=3)

1 => string ' UDP ' (length=3)

2 => String ' Unix ' (length=4)

3 => string ' UDG ' (length=3)

4 => string ' SSL ' (length=3)

5 => string ' SSLv3 ' (length=5)

6 => string ' Sslv2 ' (length=5)

7 => string ' TLS ' (length=3)

Array (size=12)

0 => string ' https ' (length=5)

1 => string ' FTPs ' (length=4)

2 => string ' Compress.zlib ' (length=13)

3 => string ' compress.bzip2 ' (length=14)

4 => string ' php ' (length=3)

5 => string ' file ' (length=4)

6 => string ' Glob ' (length=4)

7 => string ' Data ' (length=4)

8 => string ' http ' (length=4)

9 => String ' FTP ' (length=3)

=> string ' Phar ' (length=4)

One => string ' Zip ' (length=3)

Array (size=12)

0 => string ' zlib.* ' (length=6)

1 => string ' bzip2.* ' (length=7)

2 => string ' convert.iconv.* ' (length=15)

3 => string ' string.rot13 ' (length=12)

4 => string ' String.ToUpper ' (length=14)

5 => string ' String.ToLower ' (length=14)

6 => string ' String.strip_tags ' (length=17)

7 => string ' convert.* ' (length=9)

8 => string ' consumed ' (length=8)

9 => string ' Dechunk ' (length=7)

=> string ' mcrypt.* ' (length=8)

One => string ' mdecrypt.* ' (length=10)

In addition, we can customize or use a third party stream.

php://Packing Device

PHP has its own wrapper for accessing the input/output (I/O) stream. PHP has a basic php://stdin,php://stdout,php://stderr wrapper that corresponds to the default I/O resource. There is also a php://input stream, which is a read-only stream, and the stream content is the data of the POST request. This flow is especially useful when we put data in the body of a POST request to request a remote service.

Because Php://input is the most commonly used stream, here are some points of knowledge:

Copy code code as follows:

1.php://input can read post data that has not been processed. Compared to $http_raw_post_data, it brings less pressure on memory and does not require special php.ini settings. Php://input cannot be used for Enctype=multipart/form-data

2. When Content-type is application/x-www-form-urlencoded and the submission method is a post method, the $_post data is "consistent" with the Php://input data (quotes to indicate that they are in inconsistent format, Content). In other cases, they are inconsistent.

3.php://input cannot read get data. This is because the _get data is written in the path field of the HTTP request header (header) as Query_path, rather than in the body portion of the HTTP request.

Stream context (stream contexts)

This part of the content in the programming of almost no encounter, I am also more difficult to study, we are interested in the words can be their own Baidu.

Summarize

Stream in peacetime programming used not many, in the use of XML-RPC, server-side access to client data, mainly through the PHP input stream, this is a common scenario. Hackers may also use this part of the Web site when they invade it.

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.