加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP Streams(流)详细介绍及使用

发布时间:2020-12-12 20:52:45 所属栏目:PHP教程 来源:网络整理
导读:PHP Streams是内置核心操作,可能一般的开发者很少用,它用于统一文件、网络、数据压缩等类文件操作方式,并为这些类文件操作提供一组通用的函数接口。 一个stream就是一个具有流式行为的资源对象,每个stream对象都有一个包装类。Stream 可以通过 :// 方式

PHP Streams是内置核心操作,可能一般的开发者很少用,它用于统一文件、网络、数据压缩等类文件操作方式,并为这些类文件操作提供一组通用的函数接口。

一个stream就是一个具有流式行为的资源对象,每个stream对象都有一个包装类。Stream 可以通过://方式来引用。其中是包装类的名字,中的内容是由包装类的语法指定,不同的包装类的语法会有所不同。 来看看PHP 默认有哪些内置的包装类:

php [1] => file [2] => glob [3] => data [4] => http [5] => ftp [6] => zip [7] => compress.zlib [8] => https [9] => ftps [10] => phar ) */

看看。 看下面一段使用file_get_contents()获取数据的代码:

/ 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://。

手册上明确指出,可以通过 ,可以去看看手册中的例子。 OK,这里简单介绍一个PHP://,它是PHP用来处理IO流的包装类()。:

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通过,这段代码大家肯定用过:

array( 'method'=>"GET",'timeout'=>60,) ); $context = stream_context_create($opts); $html =file_get_contents('//www.52php.cn',false,$context);

(2)关于filter过滤器,首先来看看PHP有哪些内置的过滤器:

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可创建自定义的过滤器,如下:

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,"Line1n" );
fwrite ( $fp,"Word - 2n" );
fwrite ( $fp,"Easy As 123n" );

fclose ( $fp );

readfile ( "foo-bar.txt" );
/
结果如下:
LINE1
WORD - 2
EASY AS 123
/

提供PHP中streams函数列表如下:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读