当前位置: 首页 > php
  • PHP文件编辑类

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

    键盘游走者 发布于 2007-12-03 03:20php抢沙发