日期:2013-04-04  浏览次数:20506 次

作者的1.0版:

<?
/*
本代码开源,您可以对其进行修改.
下面文字请不要修改.
*********************************************
php文本数据库类1.0版
powerd by bpns
mysite:http://space.tju.cn/site/redboy/
2006-4-20
*********************************************

为了您的数据库安全,请在此程序中更改您的数据默认目录

将function TxtDB($name,$mod='w',$path='my_dbm')
中$path='my_dbm'修改成你自己设定的目录,如$path='abc123_dbm',
并将根目录下的my_dbm文件夹对行对应的更名.
*/

class TxtDB
{
  var $name='';//文本数据库名
  var $path='';
  var $minLen=20;
  var $isError;
  var $dbh;
  var $indxh;
  var $lfth;
  var $lckh;
  var $rcdCnt=0;
  var $maxID=0;
  var $leftCnt=0;
  var $DBend=0;
  var $mod='w';
  function TxtDB($name,$mod='w',$path='bpns_dbm')//修改数据库的默认文件夹在此修改
  {
    $this->name=$name;
    $this->path=$path.'/'.$name;
    $this->isError=0;
    $this->mod=$mod;
    $path=$this->path;
    if ($name!='')
    {
    @mkdir($this->path,0777);
    if (!file_exists($path.'/'.$name.'.tdb')) $this->dbh=fopen($this->path.'/'.$name.'.tdb','w+');
    else $this->dbh=fopen($path.'/'.$name.'.tdb','r+');
    if (!file_exists($path.'/'.$name.'.indx')) $this->indxh=fopen($this->path.'/'.$name.'.indx','w+');
    else $this->indxh=fopen($path.'/'.$name.'.indx','r+');
    if (!file_exists($path.'/'.$name.'.lft')) $this->lfth=fopen($this->path.'/'.$name.'.lft','w+');
    else $this->lfth=fopen($this->path.'/'.$name.'.lft','r+');
    if ($this->mod=='w')
    {
     $this->lckh=fopen($this->path.'/'.$name.'.lck','w');
     flock($this->lckh,2);
     fwrite($this->lckh,'lck');//lock the datebase
    }
    $rcd=$this->getRcd(0);
    $this->rcdCnt=$rcd[id];
    $this->maxID=$rcd[loc];
    $this->DBend=$rcd[len];
    $rcd=$this->getLeft(0);
    $this->leftCnt=$rcd[loc];
    }
    else $this->isError=1;
  }
  function setRcd($rid,$id,$loc,$len)
  {
    fseek($this->indxh,$rid*12);
    $str=pack('III',$id,$loc,$len);
    fwrite($this->indxh,$str,12);
  }
  function getRcd($rid)
  {
    fseek($this->indxh,$rid*12);
    $str=fread($this->indxh,12);
    $rcd=array();
    $rcd[id]=str2int($str);
    $rcd[loc]=str2int(substr($str,4,4));
    $rcd[len]=str2int(substr($str,8,4));
    return $rcd;
  }
  function setLeft($lid,$loc,$len)
  {
    fseek($this->lfth,$lid*8);
    $str=pack('II',$loc,$len);
    fwrite($this->lfth,$str,8);
  }
  function getLeft($lid)
  {
    fseek($this->lfth,$lid*8);
    $str=fread($this->lfth,8);
    $rcd[loc]=str2int($str);
    $rcd[len]=str2int(substr($str,4,4));
    return $rcd;
  }
  
  funct