PHP Streams (streaming) detailed introduction and use

Source: Internet
Author: User
Tags manual ftp php class readfile resource socket what php wrapper

This article mainly introduces the PHP Streams (stream) detailed introduction and use, PHP Streams is a built-in core operation, may not be commonly used by developers, it is used for unified file, network, data compression and other types of file operation mode, and for these types of file operations to provide a set of common function interface, Need friends can refer to the following

PHP streams is a built-in core operation, probably rarely used by ordinary developers, it is used for unified file, network, data compression and other types of file operations, and for these types of file operations to provide a common set of functional interfaces.

A stream is a resource object with a streaming behavior, and each stream object has a wrapper class. 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.

Look at the built-in wrapper classes that PHP defaults to:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 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 [a] => Phar) * *

Check out the PHP manual for the Protocol and wrapper classes supported by PHP.

Look at the following section of code that uses file_get_contents () to get the data:

?

1 2 3 4 5 6 7 8 9, * * 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/f Oo.txt ");  /* Read remote file from ftp.example.com using FTP */$ftpfile = file_get_contents ("ftp://user:pass@ftp.example.c Om/foo.txt ");  /* Read remote file from ftp.example.com using FTPS */$ftpsfile = file_get_contents ("Ftps://user:pass@ftp.exampl E.com/foo.txt ");

In fact ReadFile ('/path/to/somefile.txt ') or ReadFile (' file:///path/to/somefile.txt '), these two methods are equivalent. Because the default wrapper class for PHP is file://.

It is clear from the manual that you can register your own wrapper through Stream_register_wrapper () to see the examples in the manual.

OK, here's a brief introduction to a php://, which is the wrapper class that PHP uses to process IO streams (click here for an example). More powerful input and output streams can be accessed through php://:

Php://stdin: accesses the corresponding input stream for the PHP process, such as keyboard input when getting the CLI to execute the script.

Php://stdout: accesses the corresponding output stream of the PHP process.

Php://stderr: accesses the corresponding error output of the PHP process.

Php://input: A read-only stream that accesses the raw data of the request.

Php://output: Write-only data stream, written to the output area in the same way as print and echo.

PHP://FD: Allows direct access to the specified file descriptor. Example PHP://FD/3 references a file descriptor of 3.

Php://memory: Allows temporary data to be read and written. Store the data in memory.

Php://temp: IBID., will be stored in the temporary file after the amount of memory reaches a predefined limit (2MB by default).

Php://filter: Filter.

PHP can also modify and enhance the wrapper class through the context and filter.

(1) About the context, such as PHP through Stream_context_create () to set the time to get the file timeout, this code must be used:

?

1 2 3 4 5 6 7 8 $opts = Array (' http ' =>array (' method ' => ' get ', ' timeout ' =>60,)); $context = Stream_context_create ($opts); $html =file_get_contents (' http://www.jb51.net ', false, $context);

(2) With regard to filter filters, first look at what PHP has built-in filters:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 Print_r (Stream_get_filters ()); /* Array ([0] => convert.iconv.* [1] => mcrypt.* [2] => mdecrypt.* [3] => string.rot13 [4] => String.toupp ER [5] => String.ToLower [6] => string.strip_tags [7] => convert.* [8] => consumed [9] => Dechunk [a] => ; zlib.*) * * *

Custom filters can be created by Stream_filter_register () and the built-in Php_user_filter, as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

Provide a list of streams functions in PHP as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

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.