Analysis of stream in PHP and phpstreams _ PHP Tutorial

Source: Internet
Author: User
Tags wrappers
Stream analysis in PHP and phpstreams analysis. Stream analysis in PHP, phpstreams overview stream (streams) is a feature introduced by PHP4.3, mainly to unify the file, sockets and other similar resources. Analysis of stream in PHP and phpstreams

Overview

Stream (streams) is a feature introduced in PHP4.3, mainly to unify the working methods of files, sockets and other similar resources. PHP4.3 has been around for a long time, but many programmers seem to be unable to use the stream in PHP correctly. of course, this includes me. In the past, I also encountered the use of over-current in some programs, such as php: // input, but I have never had a chance to sort it out. today I will sort out this part of knowledge.

Stream is a resource provided by PHP and can be used transparently, and stream is a very powerful tool. The appropriate use of stream in the program can bring our program to a new height.

The description of streaming in the PHP Manual is as follows:
The code is as follows:
Streams were introduced with PHP 4.3.0 as a way of generalizing file, network, data compression, and other operations which share a common set of functions and uses. in its simplest definition, a stream is a resource object which exhibits streamable behavior. that is, it can be read from or written to in a linear fashion, and may be able to fseek () to an arbitrary locations within the stream.

Each stream implements a wrapper, which contains additional code for processing special protocols and codes. PHP provides 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 wrappers.

Basic stream knowledge

The PHP stream format is as follows: :// . Is the name of the package, The content depends on the different wrapper syntax.
The default package is file: //, which means that the stream is used every time we access the file system. For example, we can use the following two methods to read files: readfile ('/path/to/somefile.txt') and readfile ('file: /// path/to/somefile.txt '). read the file in the two ways and get the same result.

As mentioned above, PHP provides some built-in wrappers, protocols, and filters. You can use the following functions to check which wrappers are installed on our machine:
The code is as follows:
<? Php
Var_dump (stream_get_transports ());
Var_dump (stream_get_wrappers ());
Var_dump (stream_get_filters ());
?>

The output content of my local environment is as follows:
The code is 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)
10 => string 'phar '(length = 4)
11 => 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 'sumed' (length = 8)
9 => string 'destunk' (length = 7)
10 => string 'mcrypt. * '(length = 8)
11 => string 'mdecrypt. * '(length = 10)

In addition, we can customize or use third-party streams.

Php: // package

PHP has its own package for accessing input/output (I/O) streams. PHP has basic php: // stdin, php: // stdout, php: // stderr package corresponds to the default I/O resources. There is also a php: // input stream, which is a read-only stream and the stream content is the data of the post request. When we put data in the body of a post request to request a remote service, this stream is particularly useful.

Because php: // input is the most commonly used stream, some knowledge points are listed here:
The code is as follows:
1. php: // input can read POST data that has not been processed. Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on memory and does not require special php. ini settings. Php: // input cannot be used for enctype = multipart/form-data
2. only when Content-Type is application/x-www-form-urlencoded and the submission method is POST method, $ _ POST data and php: // the input data is "consistent" (with quotation marks, indicating that the formats are inconsistent and the content is consistent. In other cases, they are inconsistent.
3. php: // input cannot read GET data. It is because the _ GET data is written in the PATH field of the http request header as query_path, rather than in the http request body.

Stream Contexts)

This part of content has almost never been encountered in programming, and it is hard for me to study it. if you are interested, you can use Baidu.

Summary

In normal programming, the stream is not much used. when using xml-rpc, the server obtains client data mainly through php input stream input, which is a common scenario. Hackers may also use this content when they intrude into the website.

Streamams is a feature introduced in PHP4.3. it is mainly used to unify file, sockets, and other similar resources ....

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.