PHP Streams (Stream) detailed introduction and use of _php

Source: Internet
Author: User
Keywords Php streams Stream intro use
Tags readfile remove filter
PHP streams is a built-in core operation that may be rarely used by developers, and it is used for uniform file, network, data compression, and other types of file operations, and provides a common set of function interfaces for these class file operations.

A stream is a resource object with a streaming behavior, and each stream object has a wrapper class. Stream can be referenced by:// way. It is the name of the wrapper class, the contents of which are specified by the syntax of the wrapper class, and the syntax of the different wrapper classes will vary.
Let's see what the built-in wrapper classes are for PHP 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  [ten] = PHA R) */

Check out the PHP manual for the Protocol and wrapper classes for PHP support.
Look at the following code to get the data using file_get_contents ():

/* 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.tx T ");  /* 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.c Om/foo.txt ");
  

In fact ReadFile ('/path/to/somefile.txt ') or ReadFile (' file:///path/to/somefile.txt '), these two methods are equivalent. Because PHP's default wrapper class is file://.

It is clear from the manual that you can register your own wrapper with Stream_register_wrapper () to see the examples in the manual.
OK, here is a brief introduction to a php://, which is the wrapper class that PHP uses to process IO streams (see here for an example). More powerful input and output streams can be accessed via php://:

Php://stdin: Access the appropriate input stream for the PHP process, such as the keyboard input used to get the CLI to execute the script.
Php://stdout: Access the appropriate output stream for the PHP process.
Php://stderr: Access the appropriate error output for the PHP process.
Php://input: A read-only stream that accesses the raw data of the request.
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 references the file descriptor 3.
Php://memory: Allows temporary data to be read and written. Store the data in memory.
Php://temp: Ibid. will be stored in the temporary file after the amount of memory reaches the predefined limit (by default, 2MB).
Php://filter: Filter.

PHP can also be modified and enhanced by the context and filter for wrapper classes.
(1) About the context, such as PHP through Stream_context_create () to set the time to get the file timeout, this code is definitely used:

$opts = Array ('  http ' =>array (    ' method ' = ' = ' GET ',    ' timeout ' =>60,  )); $context = Stream_ Context_create ($opts); $html =file_get_contents (' http://www.bitsCN.com ', false, $context);

(2) For filter filters, let's start by looking at the built-in filters for PHP:

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  [ten] = zlib.*) */

A custom filter can be created by 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, & $c Onsumed, $closing)  {while    ($bucket = stream_bucket_make_writeable ($in)) {      $bucket data = Strtoupp ER ($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");/* results are as follows: Line1word-2easy as 123*/

Provide a list of the streams functions in PHP as follows:

Stream_bucket_append function: Add data to the queue Stream_bucket_make_writeable function: Returns a data object from the operation's queue stream_bucket_ New function: Create a fresh data stream_bucket_prepend function for the current queue: Prepare data to queue Stream_context_create function: Create a data flow context stream_context_get_ Default function: Gets the data flow context Stream_context_get_options function: Gets the setting of the data flow stream_context_set_option function: Sets the data flow, packet, or context STREAM_ Context_set_params function: Set parameter Stream_copy_to_stream function for data flow, packet, or context: Copy operations between data streams Stream_filter_ Append function: Add filter stream_filter_prepend function for data flow: Prepare a filter stream_filter_register function for Data flow: Register a filter for a data stream and execute as a PHP class STREAM_ Filter_remove function: Remove filter stream_get_contents function from a data stream: reads the remaining data in the data stream to the string Stream_get_filters function: Returns the list of data flow filters that have been registered STREAM_ Get_line function: Gets the row Stream_get_meta_data function from the data flow resource according to the given delimiter: Gets the header/metadata from the encapsulated protocol file pointer stream_get_ Transports function: Returns the registered socket transfer List Stream_get_wrappers function: Returns a list of registered data flows stream_register_ Wrapper function: Register a URL wrapper protocol implemented in PHP class Stream_select function: receive the data stream array and wait for their state to change stream_set_blocking function: Set a data flow to a blocked or non-clogging state stream_ Set_timeout function: Timeout setting for Data flow Stream_set_write_buffer function: Set buffer stream_socket_accept function for data flow: Accept by Function Stream_ Socket_server () Create a socket connection Stream_soCket_client function: Open the socket connection of the network or UNIX host Stream_socket_enable_crypto function: Turn data encryption on or off for an already connected socket stream_socket_get_ Name function: Get the names of local or network sockets Stream_socket_pair function: Create two non-differentiated socket data stream connection Stream_socket_recvfrom function: Get data from socket, Whether it's connected or not Stream_socket_sendto function: Send data to the socket, regardless of its connection Stream_socket_server function: Create a network or UNIX socket server Stream_wrapper_ Restore function: Recover a pre-logoff packet Stream_wrapper_unregister function: Unregister a URL address pack
  • 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.