Description
bool stream_wrapper_register ( string $protocol , string $classname )
To implement a wrapper, you need to define a class with a number of member functions, as defined below. When someone fopens your stream, PHP will create an instance of classname and then call methods on that instance. You must implement the methods exactly as described below - doing otherwise will lead to undefined behaviour.
Note: As of PHP 5.0.0 the instance of classname will be populated with a context property referencing a Context Resource which may be accessed with stream_context_get_options(). If no context was passed to the stream creation function, context will be set to NULL.
stream_wrapper_register() will return FALSE if the protocol already has a handler.
bool stream_open ( string $path , string $mode , int $options , string $opened_path )
mode is the mode used to open the file, as detailed for fopen(). You are responsible for checking that mode is valid for the path requested.
options holds additional flags set by the streams API. It can hold one or more of the following values OR'd together.
If the path is opened successfully, and STREAM_USE_PATH is set in options , you should set opened_path to the full path of the file/resource that was actually opened.
If the requested resource was opened successfully, you should return TRUE, otherwise you should return FALSE
void stream_close ( void )
string stream_read ( int $count )
int stream_write ( string $data )
bool stream_eof ( void )
int stream_tell ( void )
bool stream_seek ( int $offset , int $whence )
bool stream_flush ( void )
array stream_stat ( void )
resource stream_cast ( int $cast_as )
Note: Userspace wrapper stream_cast method is not supported prior to PHP 5.3.0.
bool stream_set_option ( int $option , int $arg1 , int $arg2 )
- STREAM_OPTION_BLOCKING: The method is called in response to stream_set_blocking(). arg1 is the requested blocking mode.
- STREAM_OPTION_READ_TIMEOUT: The method is called in response to stream_set_timeout(). arg1 is the timeout in seconds, and arg2 is the timeout in microseconds.
- STREAM_OPTION_WRITE_BUFFER: The method is called in response to stream_set_write_buffer(). arg1 is the buffer mode (STREAM_BUFFER_NONE or STREAM_BUFFER_FULL) and arg2 is the requested buffer size.
stream_set_option() should return FALSE on failure or if the option is not implemented, TRUE otherwise.
Note: Userspace wrapper stream_set_option method is not supported prior to PHP 5.3.0.
Note: Userspace wrapper unlink method is not supported prior to PHP 5.0.0.
bool rename (
string $path_from ,
string $path_to )
Note: Userspace wrapper rename method is not supported prior to PHP 5.0.0.
bool mkdir (
string $path ,
int $mode ,
int $options )
Note: Userspace wrapper mkdir method is not supported prior to PHP 5.0.0.
bool rmdir (
string $path ,
int $options )
Note: Userspace wrapper rmdir method is not supported prior to PHP 5.0.0.
bool dir_opendir ( string $path , int $options )
array url_stat ( string $path , int $flags )
flags holds additional flags set by the streams API. It can hold one or more of the following values OR'd together.
string dir_readdir ( void )
bool dir_rewinddir ( void )
bool dir_closedir ( void )
The example below implements a var:// protocol handler that allows read/write access to a named global variable using standard filesystem stream functions such as fread(). The var:// protocol implemented below, given the URL "var://foo" will read/write data to/from $GLOBALS["foo"].
Example #1 A Stream for reading/writing global variables
<?php
class VariableStream {
var $position;
var $varname;
function stream_open($path, $mode, $options, &$opened_path)
{
$url = parse_url($path);
$this->varname = $url["host"];
$this->position = 0;
return true;
}
function stream_read($count)
{
$ret = substr($GLOBALS[$this->varname], $this->position, $count);
$this->position += strlen($ret);
return $ret;
}
function stream_write($data)
{
$left = substr($GLOBALS[$this->varname], 0, $this->position);
$right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
$GLOBALS[$this->varname] = $left . $data . $right;
$this->position += strlen($data);
return strlen($data);
}
function stream_tell()
{
return $this->position;
}
function stream_eof()
{
return $this->position >= strlen($GLOBALS[$this->varname]);
}
function stream_seek($offset, $whence)
{
switch ($whence) {
case SEEK_SET:
if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
$this->position = $offset;
return true;
} else {
return false;
}
break;
case SEEK_CUR:
if ($offset >= 0) {
$this->position += $offset;
return true;
} else {
return false;
}
break;
case SEEK_END:
if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
$this->position = strlen($GLOBALS[$this->varname]) + $offset;
return true;
} else {
return false;
}
break;
default:
return false;
}
}
}
stream_wrapper_register("var", "VariableStream")
or die("Failed to register protocol");
$myvar = "";
$fp = fopen("var://myvar", "r+");
fwrite($fp, "line1\n");
fwrite($fp, "line2\n");
fwrite($fp, "line3\n");
rewind($fp);
while (!feof($fp)) {
echo fgets($fp);
}
fclose($fp);
var_dump($myvar);
?>