class DbStream
{
function stream_open($path, $mode, $options, &$opened_path)
{
mysql_connect('127.0.0.1','root','')))
$name = parse_url($path);
mysql_select_db($name['host'],$this->dbid);
mysql_query("SET NAMES 'cp1251'");
return true;
}
function stream_read($count)
{
$query = "SELECT `code` FROM `fake` WHERE `id`='1' LIMIT 1";
$res = mysql_query($query);
if($res)
{
$row = mysql_fetch_array($res);
echo $row[0];//2
return $row[0];//3
}else{
return 'Sh*t!'.mysql_error();
}
}
function stream_write($data)
{
return true;
}
function stream_tell()
{
return 0;
}
function stream_eof()
{
return true;
}
function stream_seek($offset, $whence)
{
return true;
}
}
if(stream_wrapper_register("mys","DbStream"))
{
echo 'WRAPPER REGISTERED<br>';
}else{
die('ERR!');
}
if(!($fp = fopen('mys://fake',"r")))
{
echo 'Cannot open connection';
}
$phpcode = fgets($fp);//1
echo $phpcode;//4
<?php
echo "Database record #1";
?>
Просто текст
<?php
class MyClass
{
function __constructor()
{
echo "MyClass constructed";
}
function print()
{
echo "А ты как думал?";
}
}
$my = new MyClass();
?>
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.
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.
class DbStream
{
function stream_open($path, $mode, $options, &$opened_path)
{
mysql_connect('127.0.0.1','root','') or die(mysql_error());
$name = parse_url($path);
mysql_select_db($name['host']);
mysql_query("SET NAMES 'cp1251'");
$query = "SELECT `code` FROM `fake` WHERE `id`='1' LIMIT 1";
$res = mysql_query($query) or die('Sh*t!'.mysql_error());
$row = mysql_fetch_array($res);
$this->buf = $row[0];
return true;
}
function stream_read($count)
{
$ret = substr($this->buf,0,$count);
$this->buf = substr($this->buf,$count);
return $ret;
}
function stream_eof()
{
return !strlen($this->buf);
}
}
if(stream_wrapper_register("mys","DbStream"))
{
echo 'WRAPPER REGISTERED<br>';
}else{
die('ERR!');
}
$fp = fopen('mys://fake','r');
fpassthru($fp);
#include 'mys://fake';