Understanding the Streams in PHP

Source: Internet
Author: User
Tags filter readfile wrapper

Streams is a powerful tool that PHP provides, and we often inadvertently use it, and if good usage will greatly increase PHP productivity. After harnessing the power of streams, the application will be elevated to a new level.

The following is a description of streams in the PHP manual:

Streams is introduced in PHP version 4.3.0, and it is used in the operation of file, network, data compression and other class files, and provides a set of common function interfaces for these file operations. In short, a stream is a resource object that has a streaming behavior. In other words, we can read and write the stream in a linear way. And you can use Fseek () to jump to any position within the stream.

Each streams object has a wrapper class that can add code to handle special protocols and encodings in the wrapper. There are some common wrapper classes already built into PHP, and we can create and register custom wrapper classes. We can even modify and enhance the wrapper class using the existing context and filter.

Stream Basic Knowledge

The Stream can be referenced by:// mode. This is the name of the wrapper class, the contents of which are specified by the syntax of the wrapper class, and the syntax of the different wrapper classes varies.

The default wrapper class for PHP is file://, which means that when we access the filesystem, we actually use a stream. There are two ways in which we can read the contents of a file, ReadFile ('/path/to/somefile.txt ') or ReadFile (' File:///path/to/somefile.txt '), which is equivalent. If you are using ReadFile (' http://google.com/'), then PHP chooses the HTTP stream wrapper class to operate.

As mentioned above, PHP offers a number of built-in package-turn classes, protocol, and filter. You can query the wrapper classes supported by this computer, as described in the following ways:

  

Print_r (Stream_get_transports ());

Print_r (Stream_get_wrappers ());

Print_r (Stream_get_filters ());

The output on my machine is:

Array

(

[0] => TCP

[1] => UDP

[2] => Unix

[3] => UDG

[4] => SSL

[5] => SSLv3

[6] => Sslv2

[7] => TLS

)

Array

(

[0] => HTTPS

[1] => FTPs

[2] => Compress.zlib

[3] => COMPRESS.BZIP2

[4] => PHP

[5] => file

[6] => Glob

[7] => data

[8] => http

[9] => FTP

[Ten] => zip

[One] => Phar

)

Array

(

[0] => zlib.*

[1] => bzip2.*

[2] => convert.iconv.*

[3] => string.rot13

[4] => String.ToUpper

[5] => String.ToLower

[6] => String.strip_tags

[7] => convert.*

[8] => consumed

[9] => Dechunk

[Ten] => mcrypt.*

[One] => mdecrypt.*

)

It offers a lot of functionality, does it look good?

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.