Learning and understanding of PHP input and output streams

Source: Internet
Author: User
Tags wrappers
Learning and understanding of PHP input and output streams

php://

php:// - access to individual input/output streams (I/O streams)

PHP provides a number of miscellaneous input/output (IO) streams that allow access to PHP's input and output streams, standard input and output and error descriptors, in-memory, disk-backed temporary file streams, and filters that can manipulate other read-write file resources.

?

Php://stdin, Php://stdout and Php://stderr

php://stdin, php://stdout and php://stderr allow direct access to the appropriate input or output stream for the PHP process. The data stream refers to the copied file descriptor, so if you open php://stdin it and then close it, only the copy is turned off, and the actual reference STDIN is not affected. Note that PHP's behavior in this area has many bugs until PHP 5.2.1. It is recommended that you STDIN simply STDOUT use STDERR constants, and instead of manually opening these wrappers.

php://stdinis read-only, php://stdout and php://stderr is write-only.

?

Php://input

php://inputis a read-only stream of raw data that can access the request. In the case of a POST request, php://input It is best to use $HTTP_RAW_POST_DATA it instead, because it does not depend on specific php.ini instructions. And, in such cases, the $HTTP_RAW_POST_DATA default is no padding, which requires less memory than the active always_populate_raw_post_data. enctype= "Multipart/form-data" php://input is invalid.

Note: php://input Open streams can only be read once, and data streams do not support seek operations. However, depending on the implementation of the SAPI, when the request body data is saved, it can open another php://input data stream and reread it. Typically, this situation is only for POST requests, not other requests, such as PUT or PROPFIND.

?

Php://output

php://outputis a write-only data stream that allows you to write to the output buffer in the same way as print and echo .

?

Php://fd

php://fdAllows direct access to the specified file descriptor. For example php://fd/3 , the file descriptor 3 is referenced.

?

Php://memory and Php://temp

php://memoryAnd php://temp is a data stream similar to a file wrapper that allows to read and write temporary data. The only difference between the two is that the php://memory data is always stored in memory, and the php://temp amount of memory reaches the predefined limit (by default, 2MB) into the temporary file. The temporary file location is determined in the same way as Sys_get_temp_dir () .

php://tempThe memory limit can be controlled by adding /maxmemory:nn ,NN is the maximum amount of data in bytes, reserved in memory, and the temporary file is used.

?

Php://filter

php://filteris a meta-wrapper designed for filtering filtering applications when data flow is turned on. This is useful for all-in-one (all-in-one) file functions, such as ReadFile (), Files ( ), and file_get_contents (), There is no opportunity to apply other filters before the data stream content is read.

php://filterThe target uses the following parameters as part of its path. The composite filter chain can be specified on a path. Use these parameters in detail to refer to specific examples.

?

php://filter Parameters Name Description
Resource= <要过滤的数据流> This parameter is required. It specifies the data stream that you want to filter for filtering.
Read= <读链的筛选列表> This parameter is optional. You can set one or more filter names to pipe breaks (| Separated
Write= <写链的筛选列表> This parameter is optional. You can set one or more filter names to pipe breaks (| Separated
<; filter List of two chains > Any filter list that does not have a prefix of read= or write= will be applied to read or write chains as appropriate.

?

Options available

encapsulates the protocol summary (for Php://filter, refer to the filtered wrapper. ) Property Support
First in Allow_url_fopen No
First in Allow_url_include Only php://input, php://stdin, php://memory , and php://temp.
Allow read Only Php://stdin, php://input, php://fd, php://memory , and php://temp.
Allow Write Only php://stdout, php://stderr, php://output, php://fd, php://memory and php://temp.
Allow Append Only php://stdout, php://stderr, php://output, php://fd, php://memory and php://temp(equals Write)
Allow simultaneous read and write Only php://fd, php://memory and php://temp.
Support stat () Only php://memory and php://temp.
Support unlink () No
Support rename () No
Support mkdir () No
Support rmdir () No
Only support Stream_select () Php://stdin, php://stdout, php://stderr, php://fd and php://temp.

?

Example

Example #1 Php://temp/maxmemory

This optional option allows you to set php://temp the maximum memory limit before you start using temporary files.

 
  

?

Example #2 php://filter/resource= <待过滤的数据流>

This parameter must be at php://filter the end and point to the data stream that needs to be filtered.

 
  

?

Example #3 php://filter/read= <读链需要应用的过滤器列表>

This parameter takes one or more filter names separated by a pipe character | .

 
  

?

Example #4 php://filter/write= <写链需要应用的过滤器列表>

This parameter takes one or more filter names separated by a pipe character | .

 
  

?

Original address: http://www.php.net/manual/zh/wrappers.php.php

?

php 3.0.13 and above, since PHP 4.3.0 support php://output and php://input, since PHP 5.0.0 Support C5>php://filter.

    • Php://stdin

    • Php://stdout

    • Php://stderr

    • Php://output

    • Php://input

    • Php://filter

Php://stdin,php://stdout , and php://stderr allow access to the appropriate input or output streams of the PHP process.

The php://output allows data to be written to the output buffering mechanism in the same way as print () and Echo () .

php://input allows you to read the original data of the POST. Compared to $HTTP _raw_post_data , it brings less pressure to memory and does not require any special php.ini settings.

Php://stdin and php://input are read-only, at the same time,php://stdout,php://stderr and php://output it's only written.

Php://filter is a wrapper protocol designed to allow a filter program to become a stream when it is opened. This is useful for individual file functions with full functionality such as ReadFile (), File() , and file_get_contents () . Otherwise, there is no chance to apply the filter to the stream before reading the content.

The target of Php://filter accepts subsequent ' parameters ' as part of its ' path '.

  • /resource= (required " This parameter must be at the end of php://filter and need to point to the stream to be filtered.

     

    ?

  • /read= (optional ) This parameter accepts the name of one or more filters, separated by the pipe character | .

    !--? php/* This would output the contents of www.example.com entirely in uppercase */  ReadFile ("php://filter/read=string.toupper/resource=http://www.example.com");/* This would do the same as above but would Also ROT13 encode it */readfile ("php://filter/read=string.toupper|string.rot13/resource=http://www.example.com");? --> 

    ?

  • /write= (optional) This parameter accepts the name of one or more filters, separated by a pipe character | .

      
        

    ?

  • / (optional) Any filter not specified by read= or write= is applied simultaneously to the read-write chain.

?

Table J-5. Wrapper Summary (for Php://filter, refer to Summary of Wrapper being filtered.)

Property Support
Restricted by Allow_url_fopen. No
Allows Reading Php://stdin and php://input only.
Allows Writing php://stdout, php://stderr, and php://output only.
Allows appending php://stdout, php://stderr, and php://output only. (equivalent to writing)
Allows simultaneous Reading and Writing No. These wrappers is unidirectional.
Supports stat () No
Supports unlink () No

?

Original: http://php.jz123.cn/wrappers.php.html

?

?

?

?

?

?

  • 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.