PHP stream_register_wrapper() Function
The PHP stream_register_wrapper() function is used to register a URL wrapper implemented as a PHP class. It allows implementing user-defined protocol handlers and streams for use with all the other filesystem functions, such as fopen(), fread() etc.
This function is an alias of stream_wrapper_register() function.
Syntax
stream_register_wrapper(protocol, class, flags)
Parameters
protocol |
Required. Specify the wrapper name to be registered. Valid protocol names must contain alphanumerics, dots (.), plusses (+), or hyphens (-) only. |
class |
Required. Specify the classname which implements the protocol. |
flags |
Required. Set to STREAM_IS_URL if protocol is a URL protocol. Default is 0, local stream. |
Return Value
Returns true on success or false on failure. Return false if the protocol already has a handler.
Example:
The example below shows how to use stream_register_wrapper() function to register a stream wrapper.
<?php class VariableStream { //codes to implement methods like //stream_open, stream_write etc. } //checking the existence of 'var' stream //wrapper, if exists unregister it $existed = in_array("var", stream_get_wrappers()); if ($existed) { stream_wrapper_unregister("var"); } //register 'var' stream wrapper stream_register_wrapper("var", "VariableStream"); //opening the file $fp = fopen("var://test.txt", "w+"); //writing some content to it fwrite($fp, "line1 content\n"); fwrite($fp, "line2 content\n"); fwrite($fp, "line3 content\n"); //getting the starting position of the file rewind($fp); //reading the content of the file while (!feof($fp)) { echo fgets($fp); } //closing the file fclose($fp); //restore the 'var' stream wrapper //if previously existed if ($existed) { stream_wrapper_restore("var"); } ?>
The output of the above code will be:
line1 content line2 content line3 content
❮ PHP Streams Reference