最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

一個(gè)stream就是一個(gè)具有流式行為的資源對(duì)象,每個(gè)stream對(duì)象都有一個(gè)包裝類(lèi)。Stream 可以通過(guò)<scheme>://<target>方式來(lái)引用。其中<scheme>是包裝類(lèi)的名字,<target>中的內(nèi)容是由包裝類(lèi)的語(yǔ)法指定,不同的包裝類(lèi)的語(yǔ)法會(huì)有所不同。
來(lái)看看PHP 默認(rèn)有哪些內(nèi)置的包裝類(lè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手冊(cè)中關(guān)于PHP支持的協(xié)議和包裝類(lèi)。
看下面一段使用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)包裝類(lèi)就是file://。

 手冊(cè)上明確指出,可以通過(guò)stream_register_wrapper()注冊(cè)自己的包裝器 ,可以去看看手冊(cè)中的例子。
 OK,這里簡(jiǎn)單介紹一個(gè)PHP://,它是PHP用來(lái)處理IO流的包裝類(lèi)(點(diǎn)擊這里看個(gè)例子)。通過(guò)PHP://可以訪(fǎng)問(wèn)更強(qiáng)大的輸入輸出流

php://stdin:訪(fǎng)問(wèn)PHP進(jìn)程相應(yīng)的輸入流,比如用在獲取cli執(zhí)行腳本時(shí)的鍵盤(pán)輸入。
php://stdout:訪(fǎng)問(wèn)PHP進(jìn)程相應(yīng)的輸出流。
php://stderr:訪(fǎng)問(wèn)PHP進(jìn)程相應(yīng)的錯(cuò)誤輸出。
php://input:訪(fǎng)問(wèn)請(qǐng)求的原始數(shù)據(jù)的只讀流。
php://output:只寫(xiě)的數(shù)據(jù)流,以 print 和 echo 一樣的方式寫(xiě)入到輸出區(qū)。
php://fd:允許直接訪(fǎng)問(wèn)指定的文件描述符。例 php://fd/3 引用了文件描述符 3。
php://memory:允許讀寫(xiě)臨時(shí)數(shù)據(jù)。 把數(shù)據(jù)儲(chǔ)存在內(nèi)存中。
php://temp:同上,會(huì)在內(nèi)存量達(dá)到預(yù)定義的限制后(默認(rèn)是 2MB)存入臨時(shí)文件中。
php://filter:過(guò)濾器。

PHP還可以通過(guò)context和filter對(duì)包裝類(lèi)進(jìn)行修飾和增強(qiáng)。
(1)關(guān)于context,如PHP通過(guò)stream_context_create()來(lái)設(shè)置獲取文件超時(shí)時(shí)間,這段代碼大家肯定用過(guò):

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'timeout'=>60,
  )
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.fzitv.net', false, $context);

 (2)關(guān)于filter過(guò)濾器,首先來(lái)看看PHP有哪些內(nèi)置的過(guò)濾器:

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.*
)
*/

 通過(guò)stream_filter_register()和內(nèi)置的php_user_filter可創(chuàng)建自定義的過(guò)濾器,如下:

/* 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ì)列中返回一個(gè)數(shù)據(jù)對(duì)象
stream_bucket_new函數(shù):為當(dāng)前隊(duì)列創(chuàng)建一個(gè)新的數(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ù):對(duì)數(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ù)流添加過(guò)濾器
stream_filter_prepend函數(shù):為數(shù)據(jù)流預(yù)備添加過(guò)濾器
stream_filter_register函數(shù):注冊(cè)一個(gè)數(shù)據(jù)流的過(guò)濾器并作為PHP類(lèi)執(zhí)行
stream_filter_remove函數(shù):從一個(gè)數(shù)據(jù)流中移除過(guò)濾器
stream_get_contents函數(shù):讀取數(shù)據(jù)流中的剩余數(shù)據(jù)到字符串
stream_get_filters函數(shù):返回已經(jīng)注冊(cè)的數(shù)據(jù)流過(guò)濾器列表
stream_get_line函數(shù):按照給定的定界符從數(shù)據(jù)流資源中獲取行
stream_get_meta_data函數(shù):從封裝協(xié)議文件指針中獲取報(bào)頭/元數(shù)據(jù)
stream_get_transports函數(shù):返回注冊(cè)的Socket傳輸列表
stream_get_wrappers函數(shù):返回注冊(cè)的數(shù)據(jù)流列表
stream_register_wrapper函數(shù):注冊(cè)一個(gè)用PHP類(lèi)實(shí)現(xiàn)的URL封裝協(xié)議
stream_select函數(shù):接收數(shù)據(jù)流數(shù)組并等待它們狀態(tài)的改變
stream_set_blocking函數(shù):將一個(gè)數(shù)據(jù)流設(shè)置為堵塞或者非堵塞狀態(tài)
stream_set_timeout函數(shù):對(duì)數(shù)據(jù)流進(jìn)行超時(shí)設(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ù):打開(kāi)網(wǎng)絡(luò)或者UNIX主機(jī)的Socket連接
stream_socket_enable_crypto函數(shù):為一個(gè)已經(jīng)連接的Socket打開(kāi)或者關(guān)閉數(shù)據(jù)加密
stream_socket_get_name函數(shù):獲取本地或者網(wǎng)絡(luò)Socket的名稱(chēng)
stream_socket_pair函數(shù):創(chuàng)建兩個(gè)無(wú)區(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)建一個(gè)網(wǎng)絡(luò)或者UNIX Socket服務(wù)端
stream_wrapper_restore函數(shù):恢復(fù)一個(gè)事先注銷(xiāo)的數(shù)據(jù)包
stream_wrapper_unregister函數(shù):注銷(xiāo)一個(gè)URL地址包

相關(guān)文章

最新評(píng)論

泾源县| 本溪市| 苗栗县| 江山市| 延吉市| 日喀则市| 辉南县| 茌平县| 迭部县| 佛学| 固镇县| 利津县| 阳春市| 叶城县| 闸北区| 偃师市| 万源市| 临高县| 平江县| 永嘉县| 龙井市| 花莲县| 康马县| 芒康县| 景洪市| 六安市| 呼伦贝尔市| 蒙城县| 杂多县| 武陟县| 唐山市| 犍为县| 连平县| 凉城县| 老河口市| 平顺县| 永靖县| 鸡东县| 桂林市| 乐亭县| 淮阳县|