濮阳杆衣贸易有限公司

主頁 > 知識庫 > 分析php://output和php://stdout的區(qū)別

分析php://output和php://stdout的區(qū)別

熱門標簽:寧波外呼營銷系統(tǒng) 地圖標注專員怎么樣 電話機器人銷售主要負責什么 房產(chǎn)中介用的是什么外呼系統(tǒng) 長沙做地圖標注公司 遼寧ai電銷機器人價格 上海做外呼線路的通信公司 四川保險智能外呼系統(tǒng)供應(yīng)商 福建銀行智能外呼系統(tǒng)價格

PHP包含了以php://開頭的一系列輸出輸出流,如php://stdin, php://stdout等。今天查看代碼時,忽然想到一個問題:php://output和php://stdout有什么區(qū)別?

從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”.

文檔并未直接闡述兩者的區(qū)別,仔細對比可得出以下信息:1. 均是只讀流; 2. php://stdin是PHP進程的標準輸入,php://input用來讀取請求正文的原始數(shù)據(jù)。通過這些信息,該如何正確認識兩者的本質(zhì)區(qū)別?

順著php://stdin進程輸入的提示,聯(lián)想PHP進程的執(zhí)行過程,再結(jié)合SAPI的差異,可以得到兩者主要區(qū)別:php://stdin是PHP進程的輸入流,執(zhí)行生命周期內(nèi)均可能有數(shù)據(jù)流入(例如CLI下的交互式輸入);php://input是PHP執(zhí)行時的外部輸入流,一般數(shù)據(jù)只能讀一次(具體看SAPI的實現(xiàn))。同理可得到php://stdout和php://output的區(qū)別:php://stdout是PHP進程的標準輸出流,php://output是返回的結(jié)果數(shù)據(jù)流。

下面用代碼驗證結(jié)論:

// 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;

命令行執(zhí)行文件,輸出如下:

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進程的標準輸出流和結(jié)果輸出流均指向終端,所有消息都打印出來。在瀏覽器端,PHP進程的輸出流被忽略,只有結(jié)果數(shù)據(jù)流被發(fā)送到web服務(wù)器。同時,print和echo調(diào)用的信息都作為執(zhí)行結(jié)果發(fā)往結(jié)果輸出流,所以都正常顯示。

最后再感慨一下PHP內(nèi)置函數(shù)的簡潔實用,一個file_put_contents函數(shù)就搞定流寫入操作,換Java需要stream/writer一堆代碼,也省去C風格的fopen/fwrite/fclose的繁瑣。

標簽:宜春 佛山 深圳 澳門 工商登記 常德 延安 宿遷

巨人網(wǎng)絡(luò)通訊聲明:本文標題《分析php://output和php://stdout的區(qū)別》,本文關(guān)鍵詞  分析,php,output,和,stdout,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《分析php://output和php://stdout的區(qū)別》相關(guān)的同類信息!
  • 本頁收集關(guān)于分析php://output和php://stdout的區(qū)別的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    孝义市| 托里县| 西贡区| 乌拉特中旗| 宁阳县| 沈丘县| 荣昌县| 炎陵县| 漠河县| 塔河县| 堆龙德庆县| 苗栗市| 泸西县| 平舆县| 安西县| 客服| 铜梁县| 吉木乃县| 阿拉善右旗| 伊宁市| 乳山市| 楚雄市| 萨迦县| 育儿| 佛坪县| 望都县| 翼城县| 阿勒泰市| 文水县| 大厂| 南木林县| 榕江县| 重庆市| 武义县| 丘北县| 四川省| 门头沟区| 清镇市| 玛纳斯县| 灵丘县| 如东县|