PHP Streams(流)詳細(xì)介紹及使用
PHP Streams是內(nèi)置核心操作,可能一般的開發(fā)者很少用,它用于統(tǒng)一文件、網(wǎng)絡(luò)、數(shù)據(jù)壓縮等類文件操作方式,并為這些類文件操作提供一組通用的函數(shù)接口。
一個stream就是一個具有流式行為的資源對象,每個stream對象都有一個包裝類。Stream 可以通過<scheme>://<target>方式來引用。其中<scheme>是包裝類的名字,<target>中的內(nèi)容是由包裝類的語法指定,不同的包裝類的語法會有所不同。
來看看PHP 默認(rèn)有哪些內(nèi)置的包裝類:
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手冊中關(guān)于PHP支持的協(xié)議和包裝類。
看下面一段使用file_get_contents()獲取數(shù)據(jù)的代碼:
/* 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" );
實(shí)際上readfile('/path/to/somefile.txt')或者readfile('file:///path/to/somefile.txt'),這兩種方式是等效的。因?yàn)镻HP的默認(rèn)包裝類就是file://。
手冊上明確指出,可以通過stream_register_wrapper()注冊自己的包裝器 ,可以去看看手冊中的例子。
OK,這里簡單介紹一個PHP://,它是PHP用來處理IO流的包裝類(點(diǎn)擊這里看個例子)。通過PHP://可以訪問更強(qiáng)大的輸入輸出流:
php://stdin:訪問PHP進(jìn)程相應(yīng)的輸入流,比如用在獲取cli執(zhí)行腳本時的鍵盤輸入。
php://stdout:訪問PHP進(jìn)程相應(yīng)的輸出流。
php://stderr:訪問PHP進(jìn)程相應(yīng)的錯誤輸出。
php://input:訪問請求的原始數(shù)據(jù)的只讀流。
php://output:只寫的數(shù)據(jù)流,以 print 和 echo 一樣的方式寫入到輸出區(qū)。
php://fd:允許直接訪問指定的文件描述符。例 php://fd/3 引用了文件描述符 3。
php://memory:允許讀寫臨時數(shù)據(jù)。 把數(shù)據(jù)儲存在內(nèi)存中。
php://temp:同上,會在內(nèi)存量達(dá)到預(yù)定義的限制后(默認(rèn)是 2MB)存入臨時文件中。
php://filter:過濾器。
PHP還可以通過context和filter對包裝類進(jìn)行修飾和增強(qiáng)。
(1)關(guān)于context,如PHP通過stream_context_create()來設(shè)置獲取文件超時時間,這段代碼大家肯定用過:
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>60,
)
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.dhdzp.com', false, $context);
(2)關(guān)于filter過濾器,首先來看看PHP有哪些內(nèi)置的過濾器:
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()和內(nèi)置的php_user_filter可創(chuàng)建自定義的過濾器,如下:
/* 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" );
/*
結(jié)果如下:
LINE1
WORD - 2
EASY AS 123
*/
提供PHP中streams函數(shù)列表如下:
stream_bucket_append函數(shù):為隊(duì)列添加數(shù)據(jù) stream_bucket_make_writeable函數(shù):從操作的隊(duì)列中返回一個數(shù)據(jù)對象 stream_bucket_new函數(shù):為當(dāng)前隊(duì)列創(chuàng)建一個新的數(shù)據(jù) stream_bucket_prepend函數(shù):預(yù)備數(shù)據(jù)到隊(duì)列 stream_context_create函數(shù):創(chuàng)建數(shù)據(jù)流上下文 stream_context_get_default函數(shù):獲取默認(rèn)的數(shù)據(jù)流上下文 stream_context_get_options函數(shù):獲取數(shù)據(jù)流的設(shè)置 stream_context_set_option函數(shù):對數(shù)據(jù)流、數(shù)據(jù)包或者上下文進(jìn)行設(shè)置 stream_context_set_params函數(shù):為數(shù)據(jù)流、數(shù)據(jù)包或者上下文設(shè)置參數(shù) stream_copy_to_stream函數(shù):在數(shù)據(jù)流之間進(jìn)行復(fù)制操作 stream_filter_append函數(shù):為數(shù)據(jù)流添加過濾器 stream_filter_prepend函數(shù):為數(shù)據(jù)流預(yù)備添加過濾器 stream_filter_register函數(shù):注冊一個數(shù)據(jù)流的過濾器并作為PHP類執(zhí)行 stream_filter_remove函數(shù):從一個數(shù)據(jù)流中移除過濾器 stream_get_contents函數(shù):讀取數(shù)據(jù)流中的剩余數(shù)據(jù)到字符串 stream_get_filters函數(shù):返回已經(jīng)注冊的數(shù)據(jù)流過濾器列表 stream_get_line函數(shù):按照給定的定界符從數(shù)據(jù)流資源中獲取行 stream_get_meta_data函數(shù):從封裝協(xié)議文件指針中獲取報(bào)頭/元數(shù)據(jù) stream_get_transports函數(shù):返回注冊的Socket傳輸列表 stream_get_wrappers函數(shù):返回注冊的數(shù)據(jù)流列表 stream_register_wrapper函數(shù):注冊一個用PHP類實(shí)現(xiàn)的URL封裝協(xié)議 stream_select函數(shù):接收數(shù)據(jù)流數(shù)組并等待它們狀態(tài)的改變 stream_set_blocking函數(shù):將一個數(shù)據(jù)流設(shè)置為堵塞或者非堵塞狀態(tài) stream_set_timeout函數(shù):對數(shù)據(jù)流進(jìn)行超時設(shè)置 stream_set_write_buffer函數(shù):為數(shù)據(jù)流設(shè)置緩沖區(qū) stream_socket_accept函數(shù):接受由函數(shù)stream_ socket_server()創(chuàng)建的Socket連接 stream_socket_client函數(shù):打開網(wǎng)絡(luò)或者UNIX主機(jī)的Socket連接 stream_socket_enable_crypto函數(shù):為一個已經(jīng)連接的Socket打開或者關(guān)閉數(shù)據(jù)加密 stream_socket_get_name函數(shù):獲取本地或者網(wǎng)絡(luò)Socket的名稱 stream_socket_pair函數(shù):創(chuàng)建兩個無區(qū)別的Socket數(shù)據(jù)流連接 stream_socket_recvfrom函數(shù):從Socket獲取數(shù)據(jù),不管其連接與否 stream_socket_sendto函數(shù):向Socket發(fā)送數(shù)據(jù),不管其連接與否 stream_socket_server函數(shù):創(chuàng)建一個網(wǎng)絡(luò)或者UNIX Socket服務(wù)端 stream_wrapper_restore函數(shù):恢復(fù)一個事先注銷的數(shù)據(jù)包 stream_wrapper_unregister函數(shù):注銷一個URL地址包
- PHP中的流(streams)淺析
- PHP中的Streams詳細(xì)介紹
- PHP Wrapper在SAE上的應(yīng)用方法
- php常用Stream函數(shù)集介紹
- php stream_get_meta_data返回值
- PHP stream_context_create()函數(shù)的使用示例
- PHP Stream_*系列函數(shù)
- PHP使用stream_context_create()模擬POST/GET請求的方法
- 深入理解PHP中的Streams工具
- PHP stream_context_create()作用和用法分析
- PHP流Streams、包裝器wrapper概念與用法實(shí)例詳解
相關(guān)文章
PHP7.1中使用openssl替換mcrypt的實(shí)例詳解
這篇文章主要介紹了PHP7.1中使用openssl替換mcrypt的實(shí)例詳解,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
yii2高級應(yīng)用之自定義組件實(shí)現(xiàn)全局使用圖片上傳功能的方法
這篇文章主要介紹了yii2高級應(yīng)用之自定義組件實(shí)現(xiàn)全局使用圖片上傳功能的方法,結(jié)合實(shí)例形式分析了Yii2自定義組件實(shí)現(xiàn)圖片上傳的相關(guān)步驟與操作技巧,需要的朋友可以參考下2016-10-10
詳解php curl帶有csrf-token驗(yàn)證模擬提交方法
這篇文章主要介紹了詳解php curl帶有csrf-token驗(yàn)證模擬提交方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
php在程序中將網(wǎng)頁生成word文檔并提供下載的代碼
在php程序文件中生成內(nèi)容到word文檔中并提供下載功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-10-10
PHP如何使用JWT做Api接口身份認(rèn)證的實(shí)現(xiàn)
這篇文章主要介紹了PHP如何使用JWT做Api接口身份認(rèn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

