A probe into PHP Stream API

Source: Internet
Author: User
Tags stream api wrappers

Like SPL, in the PHP manual, the stream is zoned "other basic Extensions", which is an easily overlooked function series in PHP development. But in fact, in C++/java, flow is a very important concept. The concept of flow originates from a pipeline in Unix, which is an uninterrupted stream of bytes used to communicate between programs and processes, or to read and write peripherals, external files, etc.


The concept of flow is introduced in PHP 4.3.0. We know that file operations, network operations, data compression operations have certain commonalities, such as linear read/write or random positioning, and flow is used to abstract these operations into a unified interface for developers to use, so "flow" is defined as a resource object which Exhibits streamable behavior.


Of course, we can further encapsulate the stream (wrapper) so that we can handle some specific protocols. For example, HTTP wrapper can translate a URL into a http/1.0 request to a file on a remote server. PHP By default has implemented a lot of wrappers, you can use Stream_get_wrappers () to get this list

The benefit of stream wrapper is that developers use a unified interface to open a connection like url,ftp without needing to care about the content of the protocol unless they implement a PHP wrapper.


Of course, in addition to these built-in PHP stream wrappers, we can add a custom stream, adding two different ways

    • Using PHP script Stream_wrapper_register () to implement
    • Write the C extension implementation by calling the PHP stream API.

The stream is used in Scheme://target, where scheme is the name of the wrapper (for example, HTTP), and target depends on a specific stream.


So what are the application scenarios for PHP stream?

Task: A file compressed in BZ2 format, the encoded format from iso-8859-1 to UTF-8, the full text into uppercase, then ROT-13 encoding, and then write a new file.


This task can be very easy if you have a brief understanding of the PHP stream

<?php/** * Example of stream filtering. * *//Open both file handles.$in= fopen (' test.txt.bz2 ',' RB ');$out= fopen (' Test-uppercase.txt ',' WB ');//ADD a decode filter to the first.Stream_filter_prepend ($in,' bzip2.decompress ', Stream_filter_read);// Change the charset from Iso-8859-1 to UTF-8Stream_filter_append ($out,' Convert.iconv.iso-8859-1/utf-8 ', Stream_filter_write);//uppercase the entire string.Stream_filter_append ($out,' String.ToUpper ', Stream_filter_write);//Run ROT-13 on the output.Stream_filter_append ($out,' string.rot13 ', Stream_filter_write);//Now copy. All of the filters is applied here.Stream_copy_to_stream ($in,$out);// clean up.Fclose$in); Fclose ($out);?>

This is because a feature in the PHP stream is-filter, and it can be filtered by the way it wants. PHP has some built-in filters that can be viewed through stream_get_filters ().

Of course, Stream wrapper also provides APIs for developers to develop some of their own filter.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.