這篇文章主要介紹了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" );新聞熱點
疑難解答