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:pass@ftp.example.com/foo.txt" );
/* Read remote file from ftp.example.com using FTPS */
$ftpsfile = file_get_contents ( "ftps://user:pass@ftp.example.com/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 , "Line1\n" );
fwrite ( $fp , "Word - 2\n" );
fwrite ( $fp , "Easy As 123\n" );
fclose ( $fp );
readfile ( "foo-bar.txt" );
/*
結果如下:
LINE1
WORD - 2
EASY AS 123
*/
|
提供PHP中streams函數列表如下:
?
|
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 |
stream_bucket_append函數:為隊列添加資料
stream_bucket_make_writeable函數:從操作的隊列中返回一個資料對象
stream_bucket_new函數:為當前隊列建立一個新的資料
stream_bucket_prepend函數:預備資料到隊列
stream_context_create函數:建立資料流上下文
stream_context_get_default函數:擷取預設的資料流上下文
stream_context_get_options函數:擷取資料流的設定
stream_context_set_option函數:對資料流、資料包或者上下文進行設定
stream_context_set_params函數:為資料流、資料包或者上下文設定參數
stream_copy_to_stream函數:在資料流之間進行複製操作
stream_filter_append函數:為資料流添加過濾器
stream_filter_prepend函數:為資料流預備添加過濾器
stream_filter_register函數:註冊一個資料流的過濾器並作為PHP類執行
stream_filter_remove函數:從一個資料流中移除過濾器
stream_get_contents函數:讀取資料流中的剩餘資料到字串
stream_get_filters函數:返回已經註冊的資料流過濾器列表
stream_get_line函數:按照給定的定界符從資料流資源中擷取行
stream_get_meta_data函數:從封裝協議檔案指標中擷取前序/中繼資料
stream_get_transports函數:返回註冊的Socket傳輸列表
stream_get_wrappers函數:返回註冊的資料流列表
stream_register_wrapper函數:註冊一個用PHP類實現的URL封裝協議
stream_select函數:接收資料流數組並等待它們狀態的改變
stream_set_blocking函數:將一個資料流設定為堵塞或者非堵塞狀態
stream_set_timeout函數:對資料流進行逾時設定
stream_set_write_buffer函數:為資料流設定緩衝區
stream_socket_accept函數:接受由函數stream_ socket_server()建立的Socket串連
stream_socket_client函數:開啟網路或者UNIX主機的Socket串連
stream_socket_enable_crypto函數:為一個已經串連的Socket開啟或者關閉資料加密
stream_socket_get_name函數:擷取本地或者網路Socket的名稱
stream_socket_pair函數:建立兩個無區別的Socket資料流串連
stream_socket_recvfrom函數:從Socket擷取資料,不管其串連與否
stream_socket_sendto函數:向Socket發送資料,不管其串連與否
stream_socket_server函數:建立一個網路或者UNIX Socket服務端
stream_wrapper_restore函數:恢複一個事先登出的資料包
stream_wrapper_unregister函數:登出一個URL地址包
|
http://www.bkjia.com/PHPjc/1000117.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1000117.htmlTechArticlePHP Streams(流)詳細介紹及使用 這篇文章主要介紹了PHP Streams(流)詳細介紹及使用,PHP Streams是內建核心操作,可能一般的開發人員很少用,它用於統...