资讯专栏INFORMATION COLUMN

php://output和php://stdout的区别

binaryTree / 1941人阅读

摘要:同理可得到和的区别是进程的标准输出流,是返回的结果数据流。在浏览器端,进程的输出流被忽略,只有结果数据流被发送到服务器。同时,和调用的信息都作为执行结果发往结果输出流,所以都正常显示。

转载请注明文章出处:https://tlanyan.me/php-output...

PHP包含了以php://开头的一系列输出输出流,如php://stdin, php://stdout等。今天查看代码时,忽然想到一个问题:php://output和php://stdout有什么区别?

从PHP的官方文献中找答案,对输入流php://stdin和php://input的解释分别如下(输出流的解释过于简略):

php://stdin

php://stdin, php://stdout and php://stderr allow direct access to the corresponding input or output stream of the PHP process. The stream references a duplicate file descriptor, so if you open php://stdin and later close it, you close only your copy of the descriptor-the actual stream referenced by STDIN is unaffected. Note that PHP exhibited buggy behavior in this regard until PHP 5.2.1. It is recommended that you simply use the constants STDIN, STDOUT and STDERR instead of manually opening streams using these wrappers.

php://stdin is read-only, whereas php://stdout and php://stderr are write-only.

php://input

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

文档并未直接阐述两者的区别,仔细对比可得出以下信息:1. 均是只读流; 2. php://stdin是PHP进程的标准输入,php://input用来读取请求正文的原始数据。通过这些信息,该如何正确认识两者的本质区别?

顺着php://stdin进程输入的提示,联想PHP进程的执行过程,再结合SAPI的差异,可以得到两者主要区别:php://stdin是PHP进程的输入流,执行生命周期内均可能有数据流入(例如CLI下的交互式输入);php://input是PHP执行时的外部输入流,一般数据只能读一次(具体看SAPI的实现)。同理可得到php://stdout和php://output的区别:php://stdout是PHP进程的标准输出流,php://output是返回的结果数据流。

下面用代码验证结论:

// file: test.php
file_put_contents("php://output", "message sent by output" . PHP_EOL);
file_put_contents("php://stdout", "message sent by stdout" . PHP_EOL);
print("message sent by print" . PHP_EOL);

echo "SAPI:" , PHP_SAPI , PHP_EOL;

命令行执行文件,输出如下:

message sent by output
message sent by stdout
message sent by print
SAPI:cli

浏览器端请求,输出如下:

message sent by output
message sent by print
SAPI:fpm-fcgi

在命令行下,PHP进程的标准输出流和结果输出流均指向终端,所有消息都打印出来。在浏览器端,PHP进程的输出流被忽略,只有结果数据流被发送到web服务器。同时,print和echo调用的信息都作为执行结果发往结果输出流,所以都正常显示。

最后再感慨一下PHP内置函数的简洁实用,一个file_put_contents函数就搞定流写入操作,换Java需要stream/writer一堆代码,也省去C风格的fopen/fwrite/fclose的繁琐。

参考

http://php.net/manual/en/wrap...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/28197.html

相关文章

  • PHP回顾之IO

    摘要:命令行时返回值为,标准输入输出均指向终端可用进程号查看。会在脚本执行完毕后关闭三个流,无需用户手动关闭。与远程网址交互是一个请求和响应的过程,其中细节可参考本人之前的文章回顾之请求和回顾之响应,也可参考协议的权威文档。 转载请注明文章出处: https://tlanyan.me/php-review... PHP回顾系列目录 PHP基础 web请求 cookie web响应 ses...

    happen 评论0 收藏0
  • PHP“流”(stream)简介

    摘要:例引用了文件描述符允许读写临时数据。注意丢失了第一个数据包输出使用过滤器在使用,,之类的函数使,可以使用过滤器应用在打开的上写入时用函数处理所有的流数据也可以使用下面的方式设置上下文我们模拟了一个包查看的会显示结果 以下内容整理添加自: Understanding Streams in PHP 基础 使用 :// 的格式来进行stream的操作。 比如使用 file:// 协议...

    kid143 评论0 收藏0
  • PHP挑战在线编程题

    摘要:今天想在中用来在线挑战一些题目,要用到标准输入输出,但最近在写又不想用来写,平时写项目都是表单提交,还真没有考虑过这个,于是看了下文档。 今天想在Sphere Online Judge中用PHP来在线挑战一些题目,要用到标准输入输出,但最近在写php又不想用c来写,平时写项目都是表单提交,还真没有考虑过这个,于是看了下文档。 文档在此:http://php.net/manual/zh/...

    JackJiang 评论0 收藏0

发表评论

0条评论

binaryTree

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<