PHP Streams (Stream) detailed introduction and use
This article mainly introduces the PHP Streams (stream) detailed introduction and use, PHP Streams is built-in core operations, it is possible that the general developers rarely use, it is used for unified file, network, data compression and other types of file operations, and for these class file operations to provide a common set of function interfaces, A friend you need can refer to the following
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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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] = Phar ) */ |
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 ():
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* 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/ba R/foo.txt "); /* Read remote file from www.example.com using HTTP */ $httpfile = file_get_contents ("Http://www.exam Ple.com/foo.txt "); /* Read remote file from www.example.com using HTTPS */ $httpsfile = file_get_contents ("https://www.e Xample.com/foo.txt "); /* Read remote file from ftp.example.com using FTP */ $ftpfile = file_get_contents ("ftp://user:pass@f Tp.example.com/foo.txt "); /* Read remote file from ftp.example.com using FTPS */ $ftpsfile = file_get_contents ("Ftps://user:pas S@ftp.example.com/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:
?
1 2 3 4 5 6 7 8 |
$opts = Array ( ' HTTP ' =>array ( ' Method ' = ' GET ', ' Timeout ' =>60, ) ); $context = Stream_context_create ($opts); $html =file_get_contents (' http://www.jb51.net ', false, $context); |
(2) For filter filters, let's start by looking at the built-in filters for PHP:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* 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 (the "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"); /* The results are as follows: LINE1 WORD-2 Easy as 123 */ |
Provide a list of the streams functions in PHP as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
Stream_bucket_append function: Adding data to a queue stream_bucket_make_writeable function: Returns a data object from the queue of operations Stream_bucket_new function: Create a new data for the current queue Stream_bucket_prepend function: Prepare data to queue Stream_context_create function: Creating a data flow context Stream_context_get_default function: Get the default data flow context stream_context_get_options function: Get the Data flow settings Stream_context_set_option function: Set the data stream, packet, or context Stream_context_set_params function: Setting parameters for Data flow, packet, or context Stream_copy_to_stream functions: Copying operations between data streams Stream_filter_append function: Adding a filter to a data stream stream_filter_prepend function: Adding filters for Data flow preparation Stream_filter_register function: Registers a filter for a data stream and executes as a PHP class Stream_filter_remove function: Removing filters from a data stream stream_get_contents function: Reads the remaining data in the data stream into a string Stream_get_filters function: Returns the list of data stream filters that have been registered Stream_get_line function: Fetching rows from a data flow resource by a given delimiter Stream_get_meta_data function: Get header/Meta data from encapsulated protocol file pointers Stream_get_transports function: Returns the registered socket transfer list Stream_get_wrappers function: Returns a list of registered data streams Stream_register_wrapper function: Register a URL wrapper protocol implemented with PHP class Stream_select function: Receives an array of data streams and waits for their state to change stream_set_blocking function: Set a data stream to be blocked or non-clogging Stream_set_timeout function: Timeout setting for data flow Stream_set_write_buffer function: Set buffer for data flow stream_socket_accept function: Accepts a socket connection created by the function Stream_ Socket_server () stream_socket_client function: Open a socket connection for a 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: Gets the name of the local or network socket Stream_socket_pair function: Create two non-differentiated socket data stream connections Stream_socket_recvfrom function: Gets data from the socket, regardless of whether it is connected or not Stream_socket_sendto function: Sends data to the socket, regardless of whether it is connected or not Stream_socket_server function: Create a network or UNIX socket server Stream_wrapper_restore function: Recovering a pre-signed packet Stream_wrapper_unregister function: Unregister a URL address pack |
http://www.bkjia.com/PHPjc/1000117.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000117.html techarticle php Streams (Stream) Detailed introduction and use this article mainly introduces the PHP Streams (stream) detailed introduction and use, PHP Streams is built-in core operations, probably the general developers rarely use, it is used in the system ...