分析php://output和php://stdout的區(qū)別
以下是【金聰采編】分享的內(nèi)容全文:
PHP包含了以php://開(kāi)頭的一系列輸出輸出流,如php://stdin, php://stdout等。今天查看代碼時(shí),忽然想到一個(gè)問(wèn)題:php://output和php://stdout有什么區(qū)別?
從PHP的官方文獻(xiàn)中找答案,對(duì)輸入流php://stdin和php://input的解釋分別如下(輸出流的解釋過(guò)于簡(jiǎn)略):
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ū)別,仔細(xì)對(duì)比可得出以下信息:1. 均是只讀流; 2. php://stdin是PHP進(jìn)程的標(biāo)準(zhǔn)輸入,php://input用來(lái)讀取請(qǐng)求正文的原始數(shù)據(jù)。通過(guò)這些信息,該如何正確認(rèn)識(shí)兩者的本質(zhì)區(qū)別?
順著php://stdin進(jìn)程輸入的提示,聯(lián)想PHP進(jìn)程的執(zhí)行過(guò)程,再結(jié)合SAPI的差異,可以得到兩者主要區(qū)別:php://stdin是PHP進(jìn)程的輸入流,執(zhí)行生命周期內(nèi)均可能有數(shù)據(jù)流入(例如CLI下的交互式輸入);php://input是PHP執(zhí)行時(shí)的外部輸入流,一般數(shù)據(jù)只能讀一次(具體看SAPI的實(shí)現(xiàn))。同理可得到php://stdout和php://output的區(qū)別:php://stdout是PHP進(jìn)程的標(biāo)準(zhǔn)輸出流,php://output是返回的結(jié)果數(shù)據(jù)流。
下面用代碼驗(yàn)證結(jié)論:
// file: test.phpfile_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 outputmessage sent by stdoutmessage sent by printSAPI:cli
瀏覽器端請(qǐng)求,輸出如下:
message sent by outputmessage sent by printSAPI:fpm-fcgi
在命令行下,PHP進(jìn)程的標(biāo)準(zhǔn)輸出流和結(jié)果輸出流均指向終端,所有消息都打印出來(lái)。在瀏覽器端,PHP進(jìn)程的輸出流被忽略,只有結(jié)果數(shù)據(jù)流被發(fā)送到web服務(wù)器。同時(shí),print和echo調(diào)用的信息都作為執(zhí)行結(jié)果發(fā)往結(jié)果輸出流,所以都正常顯示。
最后再感慨一下PHP內(nèi)置函數(shù)的簡(jiǎn)潔實(shí)用,一個(gè)file_put_contents函數(shù)就搞定流寫(xiě)入操作,換Java需要stream/writer一堆代碼,也省去C風(fēng)格的fopen/fwrite/fclose的繁瑣。
1.軟件源碼推廣展示:目的展示軟件相關(guān)功能,接收技術(shù)學(xué)習(xí)者測(cè)試、測(cè)評(píng);
2.教程課程信息展示:展示課程信息,傳授課程各階段內(nèi)容;
3.設(shè)計(jì)素材圖片展示:展示素材設(shè)計(jì)理念、思維方式、傳播設(shè)計(jì)理念;
4.福利優(yōu)惠信息展示:分享各類(lèi)最新的福利信息,各種優(yōu)惠信息展示;
以上分享目的僅供學(xué)習(xí)、參考使用,請(qǐng)勿用于其他用途,如果想商業(yè)使用或者代理,請(qǐng)自行聯(lián)系版權(quán)方獲取授權(quán)。任何未獲取授權(quán)的商業(yè)使用與本站無(wú)關(guān),請(qǐng)自行承擔(dān)相應(yīng)責(zé)任。
本站不存儲(chǔ)任何資源文件,敬請(qǐng)周知!
如果您認(rèn)為本頁(yè)信息內(nèi)容侵犯了您的相關(guān)權(quán)益(包含但不限于:著作權(quán)、首發(fā)權(quán)、隱私權(quán)等權(quán)利),或者您認(rèn)為自己是此信息的權(quán)利人但是此信息不是自己發(fā)布的,可以直接版權(quán)舉報(bào)投訴,我們會(huì)根據(jù)網(wǎng)站注冊(cè)協(xié)議、資源分享協(xié)議等協(xié)議處理,以保護(hù)您的合法權(quán)益。
本網(wǎng)站采用 BY-NC-SA 協(xié)議進(jìn)行授權(quán) 轉(zhuǎn)載請(qǐng)注明原文鏈接:分析php://output和php://stdout的區(qū)別

侵權(quán)舉報(bào)/版權(quán)申訴


