Flat File Database

{ Posted on Sep 15 2009 by admin }
Categories : Others, PHP

An easy way of creating flat file database. in my sample i use “[]” as my marker container you can also try using <> to make it look like or make it as XML file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
 
/**
* @ Author : TENG
* @ handle text file as flat file database
* @ date:  2 - 04 - 09 
*/
/* sample usage
 
	$file = 'images';
	$data = array('keywords'=>array('key1'=>'test','key2'=>'teng'),'meta_desc_id' => 5);
	$file  = new FlatFileDb($file,'./');
 
	$record = $file->getRecord('string marker here'); 
	if(!$record){
		$file->write('string marker here',$data);
	}
	$file->closefile();
	print_r($record);
*/ 
 
class FlatFileDb
{
	public $path 		= '';
	public $contents    = '';
	public $handle 		='';
	public $size 		= '';
	public $txtfile 	=''; 
	public $separator   ="\n";
 
	/**
	* @ constructor
	* @ param: $mode mode of file  when opening
	* @ param: $file filename (extension name should be excluded)
	*/
	public function __construct($file,$path,$mode='a+'){ 
		$this->path			= $path;	
		$this->txtfile		= $this->path.$file.'.txt';
		$this->handle 		= fopen($this->txtfile,$mode);
		$this->size 		= filesize($this->txtfile);
		$this->contents  	= $this->readFile();
	}
 
	/**
	* @ read the content of the text file
	*/
	public function readFile(){
		return file_get_contents($this->txtfile);
	}
 
	/**
	* @ decode an array to insert into a text file as one record
	* @ param: $array = array data
	*/ 
	public function decodeArray($array){
		 return json_encode($array);
	}
 
	/**
	* @ decode string to get array values
	* @ param: $string json decoded
	8 @ return array
	*/ 
	public function encodeString($string){
		 return json_decode($string,true);
	}
 
	/**
	* @ write string to a text file (append)
	* @ param: $record_id string that will serves as records ID
	* @ param: @array array values that content the data for each record
	*/
	public function write($record_id,$array){
		$data =  $this->decodeArray($array);
		$record_id = addslashes(strtolower($record_id));
		$string = str_replace('/','\/',$record_id);
		$sting_input = "[".$record_id."]".$data."[".$record_id."]".$this->separator;
		fwrite($this->handle, $sting_input);
	}
 
	/**
	* replace letters and format as ucwords
	*/
	public function replaceLetters($string){
		$search = array('_');
		$replace = array(' ');
		return ucwords(str_replace($search,$replace,$string));
	}
 
 
	/**
	* @ get the specific record based on the given index
	*/
	public function getRecord($string){
		$string = addslashes(strtolower($string));
		$string = str_replace('/','\/',$string);
		$pattern = '/\['.$string.'\](.*?)\['.$string.'\]/i';
		preg_match($pattern,$this->contents,$matches);
		return (isset($matches[1]))? $this->encodeString($matches[1]):false;
	}
	/**
	* @ close file 
	*/
	public function closefile(){
		fclose($this->handle);
 
	}
}
 
?>

Post a Comment