Home  >  Article  >  Backend Development  >  PHP输入输出流学习笔记_PHP

PHP输入输出流学习笔记_PHP

WBOY
WBOYOriginal
2016-06-01 11:05:48781browse

PHP输入和输出流是通过php://来访问的,它允许访问 PHP 的输入输出流、标准输入输出和错误描述符, 内存中、磁盘备份的临时文件流以及可以操作其他读取写入文件资源的过滤器。
php://stdin, php://stdout 和 php://stderr
php://stdin,php://stdout 和 php://stderr允许访问 PHP 进程相应的输入或者输出流。
php://input
php://input 是个可以访问请求的原始数据的只读流。 POST 请求的情况下,最好使用 php://input 来代替 $HTTP_RAW_POST_DATA(原生的post数据),因为它不依赖于特定的 php.ini 指令,内存消耗更少。如下例:

<?php echo file_get_contents("php://input"); ?>

结果:

php://output
php://output 是一个只写的数据流, 允许你以 print 和 echo 一样的方式 写入到输出缓冲区。
php://fd
php://fd 允许直接访问指定的文件描述符。 例如 php://fd/3 引用了文件描述符 3。
php://memory 和 php://temp
php://memory 和 php://temp 是一个类似文件 包装器的数据流,允许读写临时数据。 两者的唯一区别是 php://memory 总是把数据储存在内存中, 而 php://temp 会在内存量达到预定义的限制后(默认是 2MB)存入临时文件中。 临时文件位置的决定和 sys_get_temp_dir() 的方式一致。
php://filter
php://filter 是一种元封装器, 设计用于数据流打开时的筛选过滤应用。 这对于一体式(all-in-one)的文件函数非常有用,类似 readfile()、 file() 和 file_get_contents(), 在数据流内容读取之前没有机会应用其他过滤器。参数如下:

如下例:

<?php
/* 这会以大写字母输出 www.bitsCN.com 的全部内容 */
readfile("php://filter/read=string.toupper/resource=http://www.bitsCN.com");
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn