This article mainly introduces the detailed introduction and use of PHP Streams (stream). PHP Streams is a built-in core operation and may be rarely used by ordinary developers. It is used to unify file, network, data compression and other file-like operations, and provides a set of common function interfaces for these file-like operations. I hope to be helpful.
PHP Streams is a built-in core operation that may be rarely used by ordinary developers. It is used to unify file, network, data compression and other file-like operations, and provides a set of common functions for these file-like operations. interface.
A stream is a resource object with streaming behavior, and each stream object has a wrapper class. Stream can be referenced through
Let’s take a look at the built-in packaging classes that PHP has by default:
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 ) */
Look at the protocols supported by PHP in the PHP manual and packaging classes.
Look at the following code that uses file_get_contents() to obtain data:
/* 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" );
Actually readfile('/path/to/ somefile.txt') or readfile('file:///path/to/somefile.txt'), these two methods are equivalent. Because PHP's default packaging class is file://.
The manual clearly states that you can register your own wrapper through stream_register_wrapper(). You can check out the examples in the manual.
OK, here is a brief introduction to PHP://, which is a wrapper class used by PHP to handle IO streams (click here to see an example). More powerful input and output streams can be accessed through PHP://:
php://stdin: access the corresponding input stream of the PHP process, such as used to obtain keyboard input when cli executes a script.
php://stdout: Access the corresponding output stream of the PHP process.
php://stderr: Access the corresponding error output of the PHP process.
php://input: A read-only stream that accesses the requested raw data.
php://output: Write-only data stream, written to the output area in the same way as print and echo.
php://fd: Allows direct access to the specified file descriptor. Example php://fd/3 refers to file descriptor 3.
php://memory: Allows reading and writing temporary data. Store data in memory.
php://temp: Same as above, it will be stored in a temporary file after the amount of memory reaches the predefined limit (default is 2MB).
php://filter: filter.
PHP can also modify and enhance packaging classes through context and filter.
(1) Regarding context, for example, PHP uses stream_context_create() to set the timeout for obtaining files. You must have used this code:
$opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>60, ) ); $context = stream_context_create($opts); $html =file_get_contents('http://www.jb51.net', false, $context);
(2) Regarding filter filters, first let’s take a look at what built-in filters PHP has:
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.* ) */
Custom filters can be created through stream_filter_register() and the built-in php_user_filter, as follows:
/* 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" ); /* 结果如下: LINE1 WORD - 2 EASY AS 123 */
Provide the list of streams functions in PHP as follows:
stream_bucket_append函数:为队列添加数据 stream_bucket_make_writeable函数:从操作的队列中返回一个数据对象 stream_bucket_new函数:为当前队列创建一个新的数据 stream_bucket_prepend函数:预备数据到队列 stream_context_create函数:创建数据流上下文 stream_context_get_default函数:获取默认的数据流上下文 stream_context_get_options函数:获取数据流的设置 stream_context_set_option函数:对数据流、数据包或者上下文进行设置 stream_context_set_params函数:为数据流、数据包或者上下文设置参数 stream_copy_to_stream函数:在数据流之间进行复制操作 stream_filter_append函数:为数据流添加过滤器 stream_filter_prepend函数:为数据流预备添加过滤器 stream_filter_register函数:注册一个数据流的过滤器并作为PHP类执行 stream_filter_remove函数:从一个数据流中移除过滤器 stream_get_contents函数:读取数据流中的剩余数据到字符串 stream_get_filters函数:返回已经注册的数据流过滤器列表 stream_get_line函数:按照给定的定界符从数据流资源中获取行 stream_get_meta_data函数:从封装协议文件指针中获取报头/元数据 stream_get_transports函数:返回注册的Socket传输列表 stream_get_wrappers函数:返回注册的数据流列表 stream_register_wrapper函数:注册一个用PHP类实现的URL封装协议 stream_select函数:接收数据流数组并等待它们状态的改变 stream_set_blocking函数:将一个数据流设置为堵塞或者非堵塞状态 stream_set_timeout函数:对数据流进行超时设置 stream_set_write_buffer函数:为数据流设置缓冲区 stream_socket_accept函数:接受由函数stream_ socket_server()创建的Socket连接 stream_socket_client函数:打开网络或者UNIX主机的Socket连接 stream_socket_enable_crypto函数:为一个已经连接的Socket打开或者关闭数据加密 stream_socket_get_name函数:获取本地或者网络Socket的名称 stream_socket_pair函数:创建两个无区别的Socket数据流连接 stream_socket_recvfrom函数:从Socket获取数据,不管其连接与否 stream_socket_sendto函数:向Socket发送数据,不管其连接与否 stream_socket_server函数:创建一个网络或者UNIX Socket服务端 stream_wrapper_restore函数:恢复一个事先注销的数据包 stream_wrapper_unregister函数:注销一个URL地址包
##Related recommendations:
php file splitting and merging (resumable upload)
Using nginx+nginx-rtmp-module+ffmpeg to build a streaming media server notes (5)
Xiaowan streaming media playback-HLS streaming media on-demand system
The above is the detailed content of Detailed explanation of PHP Streams stream. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
