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://input 不能用於 enctype="multipart/form-data"

php://stdinphp://input 是唯讀的,同時 php://stdoutphp://stderrphp://output 是只寫的。

php://filter 是一種設計用來容許過濾器程式在開啟時成為流的封裝協定。這對於單獨具有完整功能的檔案函數例如 readfile()file()file_get_contents() 很有用,否則就沒有機會在讀取內容之前將過濾器套用於流之上。

php://filter 的目的接受隨後的「參數」作為其「路徑」的一部分。

  • /resource=<stream to be filtered> (required) 此參數必須位於 php://filter 的末尾並且需要指向向要過濾的流。

    <?php
    /* This is equivalent to simply:
       readfile("http://www.example.com");
       since 否 filters are actually specified */

    readfile("php://filter/resource=http://www.example.com");
    ?>

  • /read=<filter list to apply to read chain> (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=<filter list to apply to write chain> (optional) 本參數接受一個或多個過濾器的名字,用管道字元 | 分隔。

    <?php
    /* 下面的語句將使用 rot13 過濾器過濾 "Hello World"
       字串,並寫入現用的目錄下的 example.txt */
    file_put_contents("php://filter/write=string.rot13/resource=example.txt","Hello World");
    ?>

  • /<filter list to apply to both chains> (optional) 任何沒有被 read=write= 特殊的過濾器會被同時套用於讀寫鏈。

表格 M-6. 封裝協定摘要(對於 php://filter,是指被過濾的封裝器摘要。)

屬性支援
受限於 allow_url_fopen
容許讀取 僅在 php://stdinphp://input 中容許。
容許寫入 僅在 php://stdout, php://stderr, 和 php://output 中容許。
容許附加 僅在 php://stdout, php://stderr, 和 php://output 中容許 (與寫入相同)。
容許同時讀寫否。這些封裝器是單向的。
支援 stat()
支援 unlink()
支援 rename()
支援 mkdir()
支援 rmdir()