search
HomeBackend DevelopmentPHP Tutorial对PHP输入输出流学习跟认识

对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);?>

?

Example #2 php://filter/resource=

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

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

?

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");?>

?

Example #4 php://filter/write=

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

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

?

原文地址: 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");?> 

    ?

  • /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");?> 

    ?

  • /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");?> 

    ?

  • / (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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor