-
- var $file;
- var $index;
- //Create a file and write input
- function null_write($new) {
- $f=fopen($this-> file,"w");
- flock($f,LOCK_EX);
- fputs($f,$new);
- fclose($f);
- }
- // Add data records to the end of the file
- function add_write($new ) {
- $f=fopen($this->file,"a");
- flock($f,LOCK_EX);
- fputs($f,$new);
- fclose($f);
- }
- / / Used with the return of readfile() to convert a row of data into a one-dimensional array
- function make_array($line) {
- $array = explode("\x0E",$line);
- return $array;
- }
- / /Convert one row of data into a one-dimensional array
- function join_array($line) {
- $array = join("\x0E",$line); return $array;
- }
- // Return the total number of lines in the data file
- function getlines () {
- $f=file($this->file);
- return count($f);
- }
- // Return the data record of the next line (backup)
- function next_line() {
- $this-> ;index=$this->index++;
- return $this->get();
- }
- // Return the data record of the previous line (backup)
- function prev_line() {
- $this->index=$ this->index--;
- return $this->get();
- }
- // Return the data record of the current row. The data is small
- function get() {
- $f=fopen($this-> file,"r");
- flock($f,LOCK_SH);
- for($i=0;$i<=$this->index;$i++) {
- $rec=fgets($f,1024) ;
- }
- $line=explode("\x0E",$rec);
- fclose($f);
- return $line;
- }
- // Return the data record of the current line. The data is larger
- function get_big_file() {
- $f=fopen($this->file,"r");
- flock($f,LOCK_SH);
- for($i=0;$i<=$this->index;$i++) {
- $rec=fgets($f,1024*5);
- }
- $line=explode("\x0E",$rec);
- fclose($f);
- return $line;
- }
- // Open data File --- Return the file content as a one-dimensional array
- function read_file() {
- if (file_exists($this->file)) {
- $line =file($this->file);
- }
- return $ line;
- }
- // Open the data file --- return the file content as a two-dimensional array
- function openFile() {
- if (file_exists($this->file)) {
- $f =file($this-> ;file);
- $lines = array();
- foreach ($f as $rawline) {
- $tmpline = explode("\x0E",$rawline);
- array_push($lines, $tmpline);
- }
- }
- return $lines;
- }
- // Pass in an array, merge it into one line of data, and rewrite the entire file
- function overwrite($array){
- $newline = implode("\x0E",$array);
- $ f = fopen($this->file,"w");
- flock($f,LOCK_EX);
- fputs($f,$newline);
- fclose($f);
- }
- // Add a row of data Record to the end of the file
- function add_line($array,$check_n=1) {
- $s=implode("\x0E",$array);
- $f=fopen($this->file,"a");
- flock($f,LOCK_EX);
- fputs($f,$s);
- if ($check_n==1)
- fputs($f,"\n");
- fclose($f);
- }
- // Insert a row of data records to the front of the file
- function insert_line($array) {
- $newfile = implode("\x0E",$array);
- $f = fopen($this->file,"r") ;
- flock($f,LOCK_SH);
- while ($line = fgets($f,1024)) {
- $newfile .= $line;
- }
- fclose($f);
- $f = fopen($this ->file,"w");
- flock($f,LOCK_EX);
- fputs($f,$newfile);
- fclose($f);
- }
- // Update all qualified data records, suitable for situations where the byte data in each row is large
- function update($column,$query_string,$update_array) {
- $update_string = implode("\x0E",$update_array) ;
- $newfile = "";
- $fc=file($this->file);
- $f=fopen($this->file,"r");
- flock($f,LOCK_SH);
- for ($i=0;$i $list = explode("\x0E",$fc[$i]);
- if ($list[$column] != $query_string) {
- $newfile = $newfile.chop($fc[$i])."\n";
- } else {
- $newfile = $newfile.$update_string;
- }
- }
- fclose($f) ;
- $f=fopen($this->file,"w");
- flock($f,LOCK_EX);
- fputs($f,$newfile);
- fclose($f);
- }
- // Update all qualified data records, suitable for cases where the byte data in each row is small
- function update2($column,$query_string,$update_array) {
- $newline = implode("\x0E",$update_array);
- $ newfile = "";
- $f = fopen($this->file,"r");
- flock($f,LOCK_SH);
- while ($line = fgets($f,1024)) {
- $tmpLine = explode("\x0E",$line);
- if ($tmpLine[$column] == $query_string) {
- $newfile .= $newline;
- } else {
- $newfile .= $line;
- }
- }
- fclose($f);
- $f = fopen($this->file,"w");
- flock($f,LOCK_EX);
- fputs($f,$newfile);
- fclose($f ; $this->file);
- $f=fopen($this->file,"r");
- flock($f,LOCK_SH);
- for ($i=0;$i $list = explode("\x0E",$fc[$i]);
- if ($list[$column] != $query_string) {
- $newfile = $newfile.chop($ fc[$i])."\n";
- }
- }
- fclose($f);
- $f=fopen($this->file,"w");
- flock($f,LOCK_EX);
- fputs($f,$newfile);
- fclose($f);
- }
- // Delete all qualified data records, suitable for cases where the byte data per row is small
- function delete2($column,$query_string ){
- $newfile = "";
- $f = fopen($this->file,"r");
- flock($f,LOCK_SH);
- while ($line = fgets($f,1024)) {
- $tmpLine = explode("\x0E",$line);
- if ($tmpLine[$column] != $query_string) {
- $newfile .= $line;
- }
- }
- fclose($f);
- $f = fopen($this->file,"w");
- flock($f,LOCK_EX);
- fputs($f,$newfile);
- fclose($f);
- }
- //Get The maximum value of a field in a file
- function get_max_value($column) {
- $tlines = file($this->file);
- for ($i=0;$i<=count($tlines);$ i++) {
- $line=explode("\x0E",$tlines[$i]);
- $get_value[]=$line[$column];
- }
- $get_max_value = max($get_value);
- return $ get_max_value;
- }
- // Query based on whether a field in the data file contains $query_string, and return all qualified data in a two-dimensional array
- function select($column, $query_string) {
- $tline = $this-> ;openfile();
- $lines = array();
- foreach ($tline as $line) {
- if ($line[$column] == $query_string) {
- array_push($lines, $line);
- }
- }
- return $lines;
- }
- // The function is the same as function select(), the speed may be slightly improved
- function select2($column, $query_string) {
- if (file_exists($this->file)) {
- $tline = $this-> read_file();
- foreach ($tline as $tmpLine) {
- $line = $this->make_array($tmpLine);
- if ($line[$column] == $query_string) {
- $lines[]= $tmpLine;
- }
- }
- }
- return $lines;
- }
- // Query based on whether a field in the data file contains $query_string, and return the first qualified data in a one-dimensional array
- function select_line($ column, $query_string) {
- $tline = $this->read_file();
- foreach ($tline as $tmpLine) {
- $line = $this->make_array($tmpLine);
- if ($line[ $column] == $query_string) {
- return $line;
- break;
- }
- }
- }
- // select next/prev line(next_prev ==> 1/next, 2/prev) by cx
- function select_next_prev_line ($column, $query_string, $next_prev) {
- $tline = $this->read_file();
- $line_key_end = count($tline) - 1;
- $line_key = -1;
- foreach ($tline as $ tmpLine) {
- $line_key++;
- $line = $this->make_array($tmpLine);
- if ($next_prev == 1) {
- // next?
- if ($line[$column] == $query_string ) {
- if ($line_key == 0) {
- return 0;
- } else {
- $line_key_up = $line_key - 1;
- return $up_line;
- }
- } else {
- $up_line = $line;
- }
- } elseif ($next_prev == 2) {
- // prev?
- if ($line[$column] == $query_string) {
- if ($line_key == $line_key_end) {
- return 0;
- } else {
- $line_key_down = $line_key + 1;
- break;
- }
- }
- } else {
- return 0;
- }
- }
- $down_line = $this->make_array($tline[$line_key_down]);
- return $down_line ;
- }
- ?>
-
-
Copy code
|