Description
bool
stream_wrapper_register ( string protocol, string classname)
stream_wrapper_register() allows you to implement
your own protocol handlers and streams for use with all the other
filesystem functions (such as fopen(),
fread() etc_)_
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_
Nota:
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)
This method is called immediately after your stream object is
created_ path specifies the URL that was
passed to fopen() and that this object is
expected to retrieve_ You can use parse_url()
to break it apart_
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 )
This method is called when the stream is closed, using
fclose()_ You must release any resources
that were locked or allocated by the stream_
string
stream_read ( int count)
This method is called in response to fread()
and fgets() calls on the stream_ You
must return up_to count bytes of data
from the current read/write position as a string_
If there are less than count
bytes available, return as many as are available_ If no
more data is available, return either FALSE or an
empty string_
You must also update the read/write position of the stream
by the number of bytes that were successfully read_
int
stream_write ( string data)
This method is called in response to fwrite()
calls on the stream_ You should store data
into the underlying storage used by your stream_ If there is not
enough room, try to store as many bytes as possible_
You should return the number of bytes that were successfully
stored in the stream, or 0 if none could be stored_
You must also update the read/write position of the stream
by the number of bytes that were successfully written_
bool
stream_eof ( void )
This method is called in response to feof()
calls on the stream_ You should return TRUE if the read/write
position is at the end of the stream and if no more data is available
to be read, or FALSE otherwise_
int
stream_tell ( void )
This method is called in response to ftell()
calls on the stream_ You should return the current read/write
position of the stream_
bool
stream_seek ( int offset, int whence)
This method is called in response to fseek()
calls on the stream_ You should update the read/write position
of the stream according to offset and
whence_ See fseek()
for more information about these parameters_
Return TRUE if the position was updated, FALSE otherwise_
bool
stream_flush ( void )
This method is called in response to fflush()
calls on the stream_ If you have cached data in your stream
but not yet stored it into the underlying storage, you should
do so now_
Return TRUE if the cached data was successfully stored (or
if there was no data to store), or FALSE if the data could
not be stored_
array
stream_stat ( void )
This method is called in response to fstat()
calls on the stream and should return an array containing the same
values as appropriate for the stream_
bool
unlink ( string path)
This method is called in response to unlink()
calls on URL paths associated with the wrapper and should attempt
to delete the item specified by path_
It should return TRUE on success or FALSE on failure_
In order for the appropriate error message to be returned,
do not define this method if your wrapper does not support unlinking_
Nota:
Userspace wrapper unlink method is not supported prior to
PHP 5_0_0_
bool
dir_opendir ( string path, int options)
This method is called immediately when your stream object is created for
examining directory contents with opendir()_
path specifies the URL that was
passed to opendir() and that this object is
expected to explore_ You can use parse_url()
to break it apart_
string
dir_readdir ( void )
This method is called in response to readdir()
and should return a string representing the next filename in the
location opened by dir_opendir()_
bool
dir_rewinddir ( void )
This method is called in response to rewinddir()
and should reset the output generated by dir_readdir()_
i_e_: The next call to dir_readdir() should return
the first entry in the location returned by dir_opendir()_
bool
dir_closedir ( void )
This method is called in response to closedir()_
You should release any resources which were locked or allocated during
the opening and use of the directory stream_
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"]_
Ejemplo 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);
?> |
|