PHP文件编辑类

PHP代码
  1. <?php   
  2. class EditFile   
  3. {   
  4.     var $fPoint//文件指针   
  5.     var $fName;  //文件名称   
  6.     var $Count;  //文件行数   
  7.   
  8.     //按指定条件打开文件并锁定 |LOCK_SH:读|LOCK_EX:写|LOCK_UN:释放|LOCK_NB|   
  9.     function OpenFile($Method="r",$Lock=LOCK_SH)   
  10.     {   
  11.         if(($Method == 'r' or $Method == 'r+') && !file_exists($this->fName))   
  12.         {   
  13.             return false;   
  14.         }   
  15.         else  
  16.         {   
  17.             $this->fPoint = fopen($this->fName,$Method);   
  18.             flock($this->fPoint,$Lock);   
  19.             return true;   
  20.         }   
  21.     }   
  22.   
  23.     //关闭文件   
  24.     function CloseFile()   
  25.     {   
  26.         if(isset($this->fPoint))   
  27.         {   
  28.             flock($this->fPoint,LOCK_UN);   
  29.             fclose($this->fPoint);   
  30.             return true;   
  31.         }   
  32.         else  
  33.         {   
  34.             return false;   
  35.         }   
  36.     }   
  37.   
  38.     //读文件到<数组>,文件每行作数组的值,返回一维数组   
  39.     function FileToArr()   
  40.     {   
  41.         if(file_exists($this->fName))   
  42.         {   
  43.             $rArr = file($this->fName);   
  44.             return $rArr;   
  45.         }   
  46.         else  
  47.         {   
  48.             return array();   
  49.         }   
  50.     }   
  51.   
  52.     //读指定长度的文件到<字符串>,没有指定返回全部   
  53.     function FileToStr($Size)   
  54.     {   
  55.         if($this->OpenFile())   
  56.         {   
  57.             if ($Size < 1)   
  58.             {   
  59.                 $Size = filesize($this->fName);   
  60.             }   
  61.             $rStr = fread($this->fPoint,$Size);   
  62.             rewind($this->fPoint);   
  63.             $this->CloseFile();   
  64.             return $rStr;   
  65.         }   
  66.         else  
  67.         {   
  68.             return "";   
  69.         }   
  70.     }   
  71.     //更改指定行的字符   
  72.     function ModifyStr($id,$Data)   
  73.     {   
  74.         if($this->OpenFile('r+',LOCK_EX))   
  75.         {   
  76.             $Next = 0; //初始行计数   
  77.             while(!feof($this->fPoint))   
  78.             {   
  79.                 $Tell = ftell($this->fPoint); //保存开始读取的位置   
  80.                 $Temp = fgets($this->fPoint,filesize($this->fName)*1024); //逐行读取   
  81.                 $Next++;   
  82.                 if($Next == $id)   
  83.                 {   
  84.                     $Str = fread($this->fPoint,filesize($this->fName)*1024);   
  85.                     $Len = strlen($Temp);   
  86.                     fseek($this->fPoint,$Tell);  //回绕当行开始处   
  87.                     $Write = str_pad($Data,$Len,"\x0E",STR_PAD_LEFT).$Str;   
  88.                     fputs($this->fPoint,$Write); //把新串写入文件   
  89.                     break;   
  90.                 }   
  91.             }   
  92.             $this->CloseFile();   
  93.             return true;   
  94.         }   
  95.         else  
  96.         {   
  97.             return false;   
  98.         }   
  99.     }   
  100.   
  101.     //删除指定行的字符   
  102.     function DeleteStr($id,$Data)   
  103.     {   
  104.         if($this->OpenFile('r+',LOCK_EX))   
  105.         {   
  106.             $Next = 0; //初始行计数   
  107.             while(!feof($this->fPoint))   
  108.             {   
  109.                 $Tell = ftell($this->fPoint); //保存开始读取的位置   
  110.                 $Temp = fgets($this->fPoint,filesize($this->fName)*1024); //逐行读取   
  111.                 $Next++;   
  112.                 if($Next == $id)   
  113.                 {   
  114.                     $Len = strlen($Data);   
  115.                     fseek($this->fPoint,$Tell);  //回绕当行开始处   
  116.                     $Write = str_pad("\x0E",$Len,"\x0E",STR_PAD_LEFT);   
  117.                     fputs($this->fPoint,$Write); //把新串写入文件   
  118.                     break;   
  119.                 }   
  120.             }   
  121.             $this->CloseFile();   
  122.             return true;   
  123.         }   
  124.         else  
  125.         {   
  126.             return false;   
  127.         }   
  128.     }   
  129.   
  130.     function GetCount()   
  131.     {   
  132.         return count($this->FileToArr());   
  133.     }   
  134.   
  135.     //写入字符串到文件尾,如果文件不存在则新建   
  136.     function WriteToEnd($String)   
  137.     {   
  138.         if($this->OpenFile("a",LOCK_EX))   
  139.         {   
  140.             fputs($this->fPoint,$String);   
  141.             $this->CloseFile();   
  142.             return true;   
  143.         }   
  144.         else  
  145.         {   
  146.             return false;   
  147.         }   
  148.     }   
  149.   
  150.     //覆盖写入字符串,如果文件不存在则新建   
  151.     function WriteToNull($String)   
  152.     {   
  153.         if($this->OpenFile("w",LOCK_EX))   
  154.         {   
  155.             fputs($this->fPoint,$String);   
  156.             $this->CloseFile();   
  157.             return true;   
  158.         }   
  159.         else  
  160.         {   
  161.             return false;   
  162.         }   
  163.     }   
  164.   
  165. }   
  166.   
  167. ?>  
posted @ 2007-12-03 03:20  二宝的博客  阅读(215)  评论(0编辑  收藏  举报