欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2015年05月12日 10:25:53   投稿:junjie  
這篇文章主要介紹了PHP Streams(流)詳細介紹及使用,PHP Streams是內(nèi)置核心操作,可能一般的開發(fā)者很少用,它用于統(tǒng)一文件、網(wǎng)絡(luò)、數(shù)據(jù)壓縮等類文件操作方式,并為這些類文件操作提供一組通用的函數(shù)接口,需要的朋友可以參考下

PHP Streams是內(nèi)置核心操作,可能一般的開發(fā)者很少用,它用于統(tǒng)一文件、網(wǎng)絡(luò)、數(shù)據(jù)壓縮等類文件操作方式,并為這些類文件操作提供一組通用的函數(shù)接口。

一個stream就是一個具有流式行為的資源對象,每個stream對象都有一個包裝類。Stream 可以通過<scheme>://<target>方式來引用。其中<scheme>是包裝類的名字,<target>中的內(nèi)容是由包裝類的語法指定,不同的包裝類的語法會有所不同。
來看看PHP 默認有哪些內(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" );

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

 手冊上明確指出,可以通過stream_register_wrapper()注冊自己的包裝器 ,可以去看看手冊中的例子。
 OK,這里簡單介紹一個PHP://,它是PHP用來處理IO流的包裝類(點擊這里看個例子)。通過PHP://可以訪問更強大的輸入輸出流

php://stdin:訪問PHP進程相應(yīng)的輸入流,比如用在獲取cli執(zhí)行腳本時的鍵盤輸入。
php://stdout:訪問PHP進程相應(yīng)的輸出流。
php://stderr:訪問PHP進程相應(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)存量達到預(yù)定義的限制后(默認是 2MB)存入臨時文件中。
php://filter:過濾器。

PHP還可以通過context和filter對包裝類進行修飾和增強。
(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.dbjr.com.cn', 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ù):為隊列添加數(shù)據(jù) 
stream_bucket_make_writeable函數(shù):從操作的隊列中返回一個數(shù)據(jù)對象
stream_bucket_new函數(shù):為當前隊列創(chuàng)建一個新的數(shù)據(jù)
stream_bucket_prepend函數(shù):預(yù)備數(shù)據(jù)到隊列 
stream_context_create函數(shù):創(chuàng)建數(shù)據(jù)流上下文
stream_context_get_default函數(shù):獲取默認的數(shù)據(jù)流上下文
stream_context_get_options函數(shù):獲取數(shù)據(jù)流的設(shè)置
stream_context_set_option函數(shù):對數(shù)據(jù)流、數(shù)據(jù)包或者上下文進行設(shè)置
stream_context_set_params函數(shù):為數(shù)據(jù)流、數(shù)據(jù)包或者上下文設(shè)置參數(shù)
stream_copy_to_stream函數(shù):在數(shù)據(jù)流之間進行復制操作
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é)議文件指針中獲取報頭/元數(shù)據(jù)
stream_get_transports函數(shù):返回注冊的Socket傳輸列表
stream_get_wrappers函數(shù):返回注冊的數(shù)據(jù)流列表
stream_register_wrapper函數(shù):注冊一個用PHP類實現(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ù)流進行超時設(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主機的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ù):恢復一個事先注銷的數(shù)據(jù)包
stream_wrapper_unregister函數(shù):注銷一個URL地址包

相關(guān)文章

最新評論