萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP Streams(流)詳細介紹及使用

PHP Streams(流)詳細介紹及使用

   這篇文章主要介紹了PHP Streams(流)詳細介紹及使用,PHP Streams是內置核心操作,可能一般的開發者很少用,它用於統一文件、網絡、數據壓縮等類文件操作方式,並為這些類文件操作提供一組通用的函數接口,需要的朋友可以參考下

  PHP Streams是內置核心操作,可能一般的開發者很少用,它用於統一文件、網絡、數據壓縮等類文件操作方式,並為這些類文件操作提供一組通用的函數接口。

  一個stream就是一個具有流式行為的資源對象,每個stream對象都有一個包裝類。Stream 可以通過://方式來引用。其中是包裝類的名字,中的內容是由包裝類的語法指定,不同的包裝類的語法會有所不同。

  來看看PHP 默認有哪些內置的包裝類:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 print_r(stream_get_wrappers()); /* Array ( [0] => php [1] => file [2] => glob [3] => data [4] => http [5] => ftp [6] => zip [7] => compress.zlib [8] => https [9] => ftps [10] => phar ) */

  看看PHP手冊中關於PHP支持的協議和包裝類。

  看下面一段使用file_get_contents()獲取數據的代碼:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* Read local file from /home/bar */ $localfile = file_get_contents ( "/home/bar/foo.txt" );   /* Identical to above, explicitly naming FILE scheme */ $localfile = file_get_contents ( "file:///home/bar/foo.txt" );   /* Read remote file from www.example.com using HTTP */ $httpfile = file_get_contents ( "http://www.example.com/foo.txt" );   /* Read remote file from www.example.com using HTTPS */ $httpsfile = file_get_contents ( "https://www.example.com/foo.txt" );   /* Read remote file from ftp.example.com using FTP */ $ftpfile = file_get_contents ( "ftp://user:[email protected]/foo.txt" );   /* Read remote file from ftp.example.com using FTPS */ $ftpsfile = file_get_contents ( "ftps://user:[email protected]/foo.txt" );

  實際上readfile('/path/to/somefile.txt')或者readfile('file:///path/to/somefile.txt'),這兩種方式是等效的。因為PHP的默認包裝類就是file://。

  手冊上明確指出,可以通過stream_register_wrapper()注冊自己的包裝器 ,可以去看看手冊中的例子。

  OK,這裡簡單介紹一個PHP://,它是PHP用來處理IO流的包裝類(點擊這裡看個例子)。通過PHP://可以訪問更強大的輸入輸出流:

  php://stdin:訪問PHP進程相應的輸入流,比如用在獲取cli執行腳本時的鍵盤輸入。

  php://stdout:訪問PHP進程相應的輸出流。

  php://stderr:訪問PHP進程相應的錯誤輸出。

  php://input:訪問請求的原始數據的只讀流。

  php://output:只寫的數據流,以 print 和 echo 一樣的方式寫入到輸出區。

  php://fd:允許直接訪問指定的文件描述符。例 php://fd/3 引用了文件描述符 3。

  php://memory:允許讀寫臨時數據。 把數據儲存在內存中。

  php://temp:同上,會在內存量達到預定義的限制後(默認是 2MB)存入臨時文件中。

  php://filter:過濾器。

  PHP還可以通過context和filter對包裝類進行修飾和增強。

  (1)關於context,如PHP通過stream_context_create()來設置獲取文件超時時間,這段代碼大家肯定用過:

  ?

1 2 3 4 5 6 7 8 $opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>60, ) ); $context = stream_context_create($opts); $html =file_get_contents('http://www.jb51.net', false, $context);

  (2)關於filter過濾器,首先來看看PHP有哪些內置的過濾器:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 print_r(stream_get_filters()); /* Array ( [0] => convert.iconv.* [1] => mcrypt.* [2] => mdecrypt.* [3] => string.rot13 [4] => string.toupper [5] => string.tolower [6] => string.strip_tags [7] => convert.* [8] => consumed [9] => dechunk [10] => zlib.* ) */

  通過stream_filter_register()和內置的php_user_filter可創建自定義的過濾器,如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 /* Define our filter class */ class strtoupper_filter extends php_user_filter { function filter ( $in , $out , & $consumed , $closing ) { while ( $bucket = stream_bucket_make_writeable ( $in )) { $bucket -> data = strtoupper ( $bucket -> data ); $consumed += $bucket -> datalen ; stream_bucket_append ( $out , $bucket ); } return PSFS_PASS_ON ; } }   /* Register our filter with PHP */ stream_filter_register ( "strtoupper" , "strtoupper_filter" ) or die( "Failed to register filter" );   $fp = fopen ( "foo-bar.txt" , "w" );   /* Attach the registered filter to the stream just opened */ stream_filter_append ( $fp , "strtoupper" );   fwrite ( $fp , "Line1n" ); fwrite ( $fp , "Word - 2n" ); fwrite ( $fp , "Easy As 123n" );   fclose ( $fp );     readfile ( "foo-bar.txt" ); /* 結果如下: LINE1 WORD - 2 EASY AS 123 */
copyright © 萬盛學電腦網 all rights reserved