搜尋
首頁php教程php手册某文本数据库blog作者写的文本数据库操作类,很不错

数据|数据库

作者的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;
  }
  
  function clear()
  {
    $this->setRcd(0,0,0,0);
    $this->setLeft(0,0,0);
  }
  function close()
  {
    @fclose($this->dbh);
    @fclose($this->indxh);
    @fclose($this->lfth);
    @fclose($this->lckh);
  }
  
  function seekSpace($len)
  {
    $res=array('loc'=>0,'len'=>0);
    if ($this->leftCnt    $find=0;
    $min=1000000;
    for ($i=$this->leftCnt;$i>0;$i--)
    {
      $res=$this->getLeft($i);
      if ($res[len]==$len) {$find=$i;break;}
      else if($res[len]>$len)
      {
         if ($res[len]-$len         {
           $min=$res[len]-$len;
           $find=$i;
         }
      }
    }
    if ($find)
    {
      $res=$this->getLeft($find);
      if ($res[len]      {
       fseek($this->lfth,($find+1)*8);
       $str=fread($this->lfth,($this->leftCnt-$find)*8);
       fseek($this->lfth,$find*8);
       fwrite($this->lfth,$str);
       $this->leftCnt--;
       $this->setLeft(0,$this->leftCnt,0);
       return $res;
      }
      else
      {
        $rs=array();
        $rs[loc]=$res[loc];
        $rs[len]=$len;
        $res[loc]+=$len;
        $this->setLeft($find,$res[loc],$res[len]-$len);
        return $rs;
      }
    }
    else//fail
    {
      $res[len]=0;
      return $res;
    }
  }
  function insert($content,$len=0)//return with record id
  {
    $res=array('loc'=>0);
    if ($this->mod!='w') return 0;
    if (!$len) $len=strlen($content); 
    if ($lenminLen) $len=$this->minLen;
    if ($this->leftCnt) $res=$this->seekSpace($len);
    if (!$res[len])
    {
      $res[loc]=$this->DBend;
      $res[len]=$len;
    }
    if ($res[loc]+$res[len]>$this->DBend) $this->DBend=$res[loc]+$res[len];
    //echo $this->DBend.'
';
    $this->maxID++;
    $this->rcdCnt++;
    $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
    $this->setRcd($this->rcdCnt,$this->maxID,$res[loc],$res[len]);
    fseek($this->dbh,$res[loc]);
    fwrite($this->dbh,$content,$len);
    return $this->maxID;
  }
  function findByID($id)
  {
    if ($id$this->maxID or $this->rcdCnt    $left=1;
    $right=$this->rcdCnt;
    while($left    {
      $mid=(int)(($left+$right)/2);
      if ($mid==$left or $mid==$right) break;
      $rcd=$this->getRcd($mid);
      if ($rcd[id]==$id) return $mid;
      else if($id      else $left=$mid;
    }
    //$rcd=$this->getRcd($mid);
    //if ($rcd[id]==$id) return $mid;
    $rcd=$this->getRcd($left);
    if ($rcd[id]==$id) return $left;
    $rcd=$this->getRcd($right);
    if ($rcd[id]==$id) return $right;
    return 0;
  }
  function delete($id)
  {
    if ($this->mod!='w') return 0;
    $rid=$this->findByID($id);
    if (!$rid) return;
    $res=$this->getRcd($rid);
    fseek($this->indxh,($rid+1)*12);
    $str=fread($this->indxh,($this->rcdCnt-$i)*12);
    fseek($this->indxh,$rid*12);
    fwrite($this->indxh,$str);
    $this->rcdCnt--;
    if ($res[loc]+$res[len]==$this->DBend)
    {
      $this->DBend=$res[loc];
      $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
    }
    else
    {
     $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
     $this->leftCnt++;
     $this->setLeft(0,$this->leftCnt,0);
     $this->setLeft($this->leftCnt,$res[loc],$res[len]);
    }
  }
  function update($id,$newcontent,$len=0)
  {
    if ($this->mod!='w') return;
    $rid=$this->findByID($id);
    if (!$rid) return;
    if (!$len) $len=strlen($newcontent); 
    $rcd=$this->getRcd($rid);
    if ($rcd[len]    {
      $this->leftCnt++;
      $this->setLeft(0,$this->leftCnt,0);
      $this->setLeft($this->leftCnt,$rcd[loc],$rcd[len]);
      $rcd[loc]=$this->DBend;
      $rcd[len]=$len;
      $this->DBend+=$len;
      $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
      $this->setRcd($rid,$rcd[id],$rcd[loc],$rcd[len]);
    }
    fseek($this->dbh,$rcd[loc]);
    fwrite($this->dbh,$newcontent,$len);
    //echo $id.'
'.$content.'
'.$len;
  }
  function selectByRid($rid)
  {
    $res=array('id'=>0,'content'=>'');
    if ($rid$this->rcdCnt) return $res;
    else $rcd=$this->getRcd($rid);
    $res[id]=$rcd[id];
    $res[len]=$rcd[len];
    fseek($this->dbh,$rcd[loc]);
    $res[content]=fread($this->dbh,$rcd[len]);
    return $res;
  }
  function select($id)
  {
    return $this->selectByRid($this->findByID($id));
  }
  function backup()
  {
    copy($this->path.'/'.$this->name.'.tdb',$this->path.'/'.$this->name.'.tdb.bck');
    copy($this->path.'/'.$this->name.'.indx',$this->path.'/'.$this->name.'.indx.bck');
    copy($this->path.'/'.$this->name.'.lft',$this->path.'/'.$this->name.'.lft.bck');
  }
  function recover()
  {
    copy($this->path.'/'.$this->name.'.tdb.bck',$this->path.'/'.$this->name.'.tdb');
    copy($this->path.'/'.$this->name.'.indx.bck',$this->path.'/'.$this->name.'.indx');
    copy($this->path.'/'.$this->name.'.lft.bck',$this->path.'/'.$this->name.'.lft');
  }
}
?>

 

作者的2.0版,生成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;
  }
  
  function clear()
  {
    $this->setRcd(0,0,0,0);
    $this->setLeft(0,0,0);
  }
  function close()
  {
    @fclose($this->dbh);
    @fclose($this->indxh);
    @fclose($this->lfth);
    @fclose($this->lckh);
  }
  
  function seekSpace($len)
  {
    $res=array('loc'=>0,'len'=>0);
    if ($this->leftCnt    $find=0;
    $min=1000000;
    for ($i=$this->leftCnt;$i>0;$i--)
    {
      $res=$this->getLeft($i);
      if ($res[len]==$len) {$find=$i;break;}
      else if($res[len]>$len)
      {
         if ($res[len]-$len         {
           $min=$res[len]-$len;
           $find=$i;
         }
      }
    }
    if ($find)
    {
      $res=$this->getLeft($find);
      if ($res[len]      {
       fseek($this->lfth,($find+1)*8);
       $str=fread($this->lfth,($this->leftCnt-$find)*8);
       fseek($this->lfth,$find*8);
       fwrite($this->lfth,$str);
       $this->leftCnt--;
       $this->setLeft(0,$this->leftCnt,0);
       return $res;
      }
      else
      {
        $rs=array();
        $rs[loc]=$res[loc];
        $rs[len]=$len;
        $res[loc]+=$len;
        $this->setLeft($find,$res[loc],$res[len]-$len);
        return $rs;
      }
    }
    else//fail
    {
      $res[len]=0;
      return $res;
    }
  }
  function insert($content,$len=0)//return with record id
  {
    $res=array('loc'=>0);
    if ($this->mod!='w') return 0;
    if (!$len) $len=strlen($content); 
    if ($lenminLen) $len=$this->minLen;
    if ($this->leftCnt) $res=$this->seekSpace($len);
    if (!$res[len])
    {
      $res[loc]=$this->DBend;
      $res[len]=$len;
    }
    if ($res[loc]+$res[len]>$this->DBend) $this->DBend=$res[loc]+$res[len];
    //echo $this->DBend.'
';
    $this->maxID++;
    $this->rcdCnt++;
    $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
    $this->setRcd($this->rcdCnt,$this->maxID,$res[loc],$res[len]);
    fseek($this->dbh,$res[loc]);
    fwrite($this->dbh,$content,$len);
    return $this->maxID;
  }
  function findByID($id)
  {
    if ($id$this->maxID or $this->rcdCnt    $left=1;
    $right=$this->rcdCnt;
    while($left    {
      $mid=(int)(($left+$right)/2);
      if ($mid==$left or $mid==$right) break;
      $rcd=$this->getRcd($mid);
      if ($rcd[id]==$id) return $mid;
      else if($id      else $left=$mid;
    }
    //$rcd=$this->getRcd($mid);
    //if ($rcd[id]==$id) return $mid;
    $rcd=$this->getRcd($left);
    if ($rcd[id]==$id) return $left;
    $rcd=$this->getRcd($right);
    if ($rcd[id]==$id) return $right;
    return 0;
  }
  function delete($id)
  {
    if ($this->mod!='w') return 0;
    $rid=$this->findByID($id);
    if (!$rid) return;
    $res=$this->getRcd($rid);
    fseek($this->indxh,($rid+1)*12);
    $str=fread($this->indxh,($this->rcdCnt-$i)*12);
    fseek($this->indxh,$rid*12);
    fwrite($this->indxh,$str);
    $this->rcdCnt--;
    if ($res[loc]+$res[len]==$this->DBend)
    {
      $this->DBend=$res[loc];
      $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
    }
    else
    {
     $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
     $this->leftCnt++;
     $this->setLeft(0,$this->leftCnt,0);
     $this->setLeft($this->leftCnt,$res[loc],$res[len]);
    }
  }
  function update($id,$newcontent,$len=0)
  {
    if ($this->mod!='w') return;
    $rid=$this->findByID($id);
    if (!$rid) return;
    if (!$len) $len=strlen($newcontent); 
    $rcd=$this->getRcd($rid);
    if ($rcd[len]    {
      $this->leftCnt++;
      $this->setLeft(0,$this->leftCnt,0);
      $this->setLeft($this->leftCnt,$rcd[loc],$rcd[len]);
      $rcd[loc]=$this->DBend;
      $rcd[len]=$len;
      $this->DBend+=$len;
      $this->setRcd(0,$this->rcdCnt,$this->maxID,$this->DBend);
      $this->setRcd($rid,$rcd[id],$rcd[loc],$rcd[len]);
    }
    fseek($this->dbh,$rcd[loc]);
    fwrite($this->dbh,$newcontent,$len);
    //echo $id.'
'.$content.'
'.$len;
  }
  function selectByRid($rid)
  {
    $res=array('id'=>0,'content'=>'');
    if ($rid$this->rcdCnt) return $res;
    else $rcd=$this->getRcd($rid);
    $res[id]=$rcd[id];
    $res[len]=$rcd[len];
    fseek($this->dbh,$rcd[loc]);
    $res[content]=fread($this->dbh,$rcd[len]);
    //$res[rid]=$rid;
    return $res;
  }
  function select($id)
  {
    return $this->selectByRid($this->findByID($id));
  }
  function backup()
  {
    copy($this->path.'/'.$this->name.'.tdb',$this->path.'/'.$this->name.'.tdb.bck');
    copy($this->path.'/'.$this->name.'.indx',$this->path.'/'.$this->name.'.indx.bck');
    copy($this->path.'/'.$this->name.'.lft',$this->path.'/'.$this->name.'.lft.bck');
  }
  function recover()
  {
    copy($this->path.'/'.$this->name.'.tdb.bck',$this->path.'/'.$this->name.'.tdb');
    copy($this->path.'/'.$this->name.'.indx.bck',$this->path.'/'.$this->name.'.indx');
    copy($this->path.'/'.$this->name.'.lft.bck',$this->path.'/'.$this->name.'.lft');
  }
}

?>


 



陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具