从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的烦琐。
引荐教程:PHP视频教程
以上就是php://output和php://stdout的区分的细致内容,更多请关注ki4网别的相干文章!