Table of Contents
php://
Home Backend Development PHP Tutorial 对PHP输入输出流学习跟认识

对PHP输入输出流学习跟认识

Jun 13, 2016 am 11:51 AM
example filter php readfile resource

对PHP输入输出流学习和认识

php://

php://访问各个输入/输出流(I/O streams)

PHP 提供了一些杂项输入/输出(IO)流,允许访问 PHP 的输入输出流、标准输入输出和错误描述符, 内存中、磁盘备份的临时文件流以及可以操作其他读取写入文件资源的过滤器。

?

php://stdin, php://stdout 和 php://stderr

php://stdinphp://stdoutphp://stderr 允许直接访问 PHP 进程相应的输入或者输出流。 数据流引用了复制的文件描述符,所以如果你打开 php://stdin 并在之后关了它, 仅是关闭了复制品,真正被引用的 STDIN 并不受影响。 注意 PHP 在这方面的行为有很多 BUG 直到 PHP 5.2.1。 推荐你简单使用常量 STDINSTDOUTSTDERR 来代替手工打开这些封装器。

php://stdin 是只读的, php://stdoutphp://stderr 是只写的。

?

php://input

php://input 是个可以访问请求的原始数据的只读流。 POST 请求的情况下,最好使用 php://input 来代替 $HTTP_RAW_POST_DATA,因为它不依赖于特定的 php.ini 指令。 而且,这样的情况下 $HTTP_RAW_POST_DATA 默认没有填充, 比激活 always_populate_raw_post_data 潜在需要更少的内存。 enctype="multipart/form-data" 的时候 php://input 是无效的。

Note: php://input 打开的数据流只能读取一次; 数据流不支持 seek 操作。 不过,依赖于 SAPI 的实现,请求体数据被保存的时候, 它可以打开另一个 php://input 数据流并重新读取。 通常情况下,这种情况只是针对 POST 请求,而不是其他请求方式,比如 PUT 或者 PROPFIND。

?

php://output

php://output 是一个只写的数据流, 允许你以 printecho 一样的方式 写入到输出缓冲区。

?

php://fd

php://fd 允许直接访问指定的文件描述符。 例如 php://fd/3 引用了文件描述符 3。

?

php://memory 和 php://temp

php://memoryphp://temp 是一个类似文件 包装器的数据流,允许读写临时数据。 两者的唯一区别是 php://memory 总是把数据储存在内存中, 而 php://temp 会在内存量达到预定义的限制后(默认是 2MB)存入临时文件中。 临时文件位置的决定和 sys_get_temp_dir() 的方式一致。

php://temp 的内存限制可通过添加 /maxmemory:NN 来控制,NN 是以字节为单位、保留在内存的最大数据量,超过则使用临时文件。

?

php://filter

php://filter 是一种元封装器, 设计用于数据流打开时的筛选过滤应用。 这对于一体式(all-in-one)的文件函数非常有用,类似 readfile()file()file_get_contents(), 在数据流内容读取之前没有机会应用其他过滤器。

php://filter 目标使用以下的参数作为它路径的一部分。 复合过滤链能够在一个路径上指定。详细使用这些参数可以参考具体范例。

?

php://filter 参数名称描述
resource= 这个参数是必须的。它指定了你要筛选过滤的数据流。
read= 该参数可选。可以设定一个或多个过滤器名称,以管道符(|)分隔。
write= 该参数可选。可以设定一个或多个过滤器名称,以管道符(|)分隔。
任何没有以 read=write= 作前缀 的筛选器列表会视情况应用于读或写链。

?

可选项

封装协议摘要(针对 php://filter,参考被筛选的封装器。) 属性支持
首先于 allow_url_fopen No
首先于 allow_url_include php://inputphp://stdinphp://memoryphp://temp
允许读取 php://stdinphp://inputphp://fdphp://memoryphp://temp
允许写入 php://stdoutphp://stderrphp://outputphp://fdphp://memoryphp://temp
允许追加 php://stdoutphp://stderrphp://outputphp://fdphp://memoryphp://temp(等于写入)
允许同时读写 php://fdphp://memoryphp://temp
支持 stat() php://memoryphp://temp
支持 unlink() No
支持 rename() No
支持 mkdir() No
支持 rmdir() No
仅仅支持 stream_select() php://stdinphp://stdoutphp://stderrphp://fdphp://temp

?

范例

Example #1 php://temp/maxmemory

这个可选选项允许设置 php://temp 开始使用临时文件前的最大内存限制。

<?php// Set the limit to 5 MB.$fiveMBs = 5 * 1024 * 1024;$fp = fopen("php://temp/maxmemory:$fiveMBs", 'r+');fputs($fp, "hello\n");// Read what we have written.rewind($fp);echo stream_get_contents($fp);?>
Copy after login

?

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

这个参数必须位于 php://filter 的末尾,并且指向需要过滤筛选的数据流。

<?php/* 这简单等同于:  readfile("http://www.example.com");  实际上没有指定过滤器 */readfile("php://filter/resource=http://www.example.com");?>
Copy after login

?

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

这个参数采用一个或以管道符 | 分隔的多个过滤器名称。

<?php/* 这会以大写字母输出 www.example.com 的全部内容 */readfile("php://filter/read=string.toupper/resource=http://www.example.com");/* 这会和以上所做的一样,但还会用 ROT13 加密。 */readfile("php://filter/read=string.toupper|string.rot13/resource=http://www.example.com");?>
Copy after login

?

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

这个参数采用一个或以管道符 | 分隔的多个过滤器名称。

<?php/* 这会通过 rot13 过滤器筛选出字符 "Hello World"  然后写入当前目录下的 example.txt */file_put_contents("php://filter/write=string.rot13/resource=example.txt","Hello World");?>
Copy after login

?

原文地址: http://www.php.net/manual/zh/wrappers.php.php

?

PHP 3.0.13 及以上版本,自 PHP 4.3.0 起支持 php://outputphp://input,自 PHP 5.0.0 起支持 php://filter

  • php://stdin

  • php://stdout

  • php://stderr

  • php://output

  • php://input

  • php://filter

php://stdinphp://stdoutphp://stderr 允许访问 PHP 进程相应的输入或者输出流。

php://output 允许向输出缓冲机制写入数据,和 print()echo() 的方式相同。

php://input 允许您读取 POST 的原始数据。 和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,并且不需要任何特殊的 php.ini 设置。

php://stdinphp://input 是只读的,同时,php://stdoutphp://stderrphp://output 是只写的。

php://filter 是一种设计用来允许过滤器程序在打开时成为流的封装协议。这对于单独具有完整功能的文件函数例如 readfile()file()file_get_contents() 很有用,否则就没有机会在读取内容之前将过滤器应用于流之上。

php://filter 的目标接受随后的'参数'作为其'路径'的一部分。

  • /resource= (required) 此参数必须位于 php://filter 的末尾并且需要指向向要过滤的流。

    <?php/* This is equivalent to simply:   readfile("http://www.example.com");   since no filters are actually specified */readfile("php://filter/resource=http://www.example.com");?> 
    Copy after login

    ?

  • /read= (optional) 本参数接受一个或多个过滤器的名字,用管道字符 | 分隔。

    <?php/* This will output the contents of   www.example.com entirely in uppercase */readfile("php://filter/read=string.toupper/resource=http://www.example.com");/* This will do the same as above   but will also ROT13 encode it */readfile("php://filter/read=string.toupper|string.rot13/resource=http://www.example.com");?> 
    Copy after login

    ?

  • /write= (optional) 本参数接受一个或多个过滤器的名字,用管道字符 | 分隔。

    <?php/* This will filter the string "Hello World"   through the rot13 filter, then write to   example.txt in the current directory */file_set_contents("php://filter/write=string.rot13/resource=example.txt","Hello World");?> 
    Copy after login

    ?

  • / (optional) 任何没有被 read=write= 指定的过滤器会被同时应用于读写链。

  • ?

    表格 J-5. Wrapper Summary (For php://filter, refer to summary of wrapper being filtered.)

    属性支持
    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 are unidirectional.
    Supports stat() No
    Supports unlink() No

    ?

    原文:http://php.jz123.cn/wrappers.php.html

    ?

    ?

    ?

    ?

    ?

    ?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles