tristram
Guest
готовый скрипт txt-базы.
это самый быстрый скрипт (из мною разобраных (около 100)) реализующий txt-базу.
особености:
1. файл открывается один раз.
2. удобная блокировка/разблокировка
3. base64 - для того чтобы не лазили уроды разные своими шАлАвливАми ручками.
это самый быстрый скрипт (из мною разобраных (около 100)) реализующий txt-базу.
особености:
1. файл открывается один раз.
2. удобная блокировка/разблокировка
3. base64 - для того чтобы не лазили уроды разные своими шАлАвливАми ручками.
PHP:
<?
class mydb_sdb
{
var $path;
var $fp;
var $db;
var $base64 = false;
function mydb_sdb($path="",$base64=true)
{
$this->load($path,$base64);
}
function openstream($path)
{
$this->closestream();
if (!is_readable($path))
{
$a = @fopen($path,"w");
@fclose($a);
}
if (!is_readable($path))
{
return false;
}
else
{
$this->fp = @fopen($path,"r+");
return true;
}
}
function lock()
{
if (is_resource($this->fp))
{
flock($this->fp,LOCK_SH);
flock($this->fp,LOCK_EX);
}
}
function unlock()
{
if (is_resource($this->fp))
{
flock($this->fp,LOCK_UN);
}
}
function closestream()
{
if (is_resource($this->fp))
{
fclose($this->fp);
}
}
function load($path,$base64=TRUE)
{
$this->path = $path;
if (empty($base64))
{
$base64 = $this->base64;
}
else
{
$this->base64 = $base64;
}
if ($this->openstream($path))
{
$data = @fread($this->fp,filesize($path));
if ($this->base64)
{
$data = base64_decode($data);
}
$this->db = unserialize($data);
return true;
}
else {return false;}
}
function loaddata($data)
{
if ($this->base64)
{
$data = base64_decode($data);
}
$this->db = unserialize($data);
}
function flush($path="")
{
if (empty($path))
{
$path = $this->path;
}
if (!is_resource($this->fp))
{
$this->openstream($path);
}
if (is_resource($this->fp))
{
$data = serialize($this->db);
if ($this->base64)
{
$data = base64_encode($data);
}
fseek($this->fp,0);
fwrite($this->fp,$data);
ftruncate($this->fp,strlen($data));
return $this->fp;
}
else {return false;}
}
function unload()
{
unset($this->db);
$this->closestream();
}
}
?>