Streams in PHP

Source: Internet
Author: User
This article mainly introduces the Streams in PHP in detail. This article introduces the basic knowledge of Stream, php: Streams package, Stream context, and so on. For more information, see

This article mainly introduces the Streams in PHP in detail. This article introduces the basic knowledge of Stream, php: // Streams package, Stream context, and so on. For more information, see

Streams is a powerful tool provided by PHP. We often use it inadvertently. If it is used properly, PHP productivity will be greatly improved. With the power of Streams, applications will be elevated to a new level.

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

The Code is as follows:


Streams was introduced in PHP 4.3.0. It is used to operate files in a unified way, such as files, networks, and data compression, provides a set of common function interfaces for these file operations. In short, a stream is a resource object with streaming behaviors. That is to say, we can use a linear method to read and write streams. You can also use fseek () to jump to any position in the stream.

Each Streams object has a packaging class. You can add Code related to special protocols and codes to the packaging. PHP has built-in some common packaging classes. We can also create and register custom packaging classes. We can even use the existing context and filter to modify and enhance the packaging class.

Basic Stream knowledge

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.

The default PHP packaging class is file: //. That is to say, when we access the file system, we actually use a stream. We can read the content in the file in the following two ways: readfile ('/path/to/somefile.txt') or readfile ('file: /// path/to/somefile.txt '). The two methods are equivalent. If you use readfile (''), PHP selects the HTTP stream packaging class for operations.

As mentioned above, PHP provides many built-in conversion classes, protocols and filters. You can query the packages supported by the local machine as described below:

The Code is as follows:


<? Php
Print_r (stream_get_transports ());
Print_r (stream_get_wrappers ());
Print_r (stream_get_filters ());

The output result on my machine is:

The Code is as follows:


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
[10] => zip
[11] => 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
[10] => mcrypt .*
[11] => mdecrypt .*
)

Many features are provided. Does it look good?

In addition to the built-in Stream above, we can also write more third-party streams for Amazon S3, MS Excel, Google Storage, Dropbox and even Twitter.

Php: // Streams packaging class

PHP has built-in packaging classes for processing I/O stream. Php: // stdin, php: // stdout, and php: // stderr, these three streams are mapped to the default I/O resources respectively. PHP also provides php: // input, which can be used to access the raw body in the POST request in read-only mode. This is a very useful feature, especially when processing remote services that embed data loads into POST requests.

Here we will use the cURL tool for a simple test:

The Code is as follows:


Curl-d "Hello World"-d "foo = bar & name = John"


The test results of using print_r ($ _ POST) in a PHP script are as follows:

The Code is as follows:


Array
(
[Foo] => bar
[Name] => John
)


We note that $ _ POST array cannot access the first item of data. However, if we use readfile ('php: // input'), the results will be different:

The Code is as follows:


Hello World & foo = bar & name = John


PHP 5.1 adds the php: // memory and php: // tempstream conversion classes to read and write temporary data. As the packaging class name implies, the data is stored in the memory or temporary files of the underlying system.

Php: // filter is a meta-packaging class used to add the filter function for stream. When you use readfile () or file_get_contents ()/stream_get_contents () to open stream, the filter will be enabled. The following is an example:

The Code is as follows:


<? Php
// Write encoded data
File_put_contents ("php: // filter/write = string. rot13/resource = file: // path/to/somefile.txt", "Hello World ");

// Read data and encode/decode
Readfile ("php: // filter/read = string. toupper | string. rot13/resource = http://www.google.com ");

In the first example, a filter is used to encode the data stored on the disk. In the second example, two cascade filters are used to read data from the remote URL. Using filters can provide extremely powerful functions for your applications.

Stream Context

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.