its a simple flatfile Class for Managinig contents of Files
Code:
<?php //developed by shushant //created on 28/11/2012 //version v 0.1 //namespace FlatFile; class DB { const PRESERVE = false; public static $getFile = null; public static $getLines = array(); public function __construct ($file){ self::$getFile = $file; //new self(); self::$getLines = self::getIdLinesByFile ($file); } public function __destruct() { self::$getLines = NULL; self::$getFile = array(); } public static function getIdLinesByFile ($file){ return explode ( "\n" , file_get_contents ($file)); } public function getAppendLine ($i, array $array){ $this->getChange(array_splice (self::$getLines, $i, 0, $array), DB::PRESERVE); } public function getChange($text, $preserve = true) { if (!$preserve) { // self::$getLines [$this -> getCount () + 1] = $text; $temp = self::$getLines; $temp [$this -> getCount () + 1] = $text; } else $temp = $text; $fh = @fopen(self::$getFile, 'w'); if (flock ($fh, LOCK_EX)) { fwrite ($fh, implode ( "\n" , $temp)); flock ($fh, LOCK_UN); //self::getIdLinesByFile (self::$getFile); // self::$getLines = (array)$temp; return 1; } fclose ($fh); unset ($temp); //free up the resources return 0; } public function getOutput() { $i = 0; foreach (self::getIdLinesByFile (self::$getFile) AS $id) { echo 'Line no '.$i.' => '.htmlspecialchars ($id) . "\n"; $i++; } } public function getCount() { return count (self::$getLines); } public function getBetween($start = 1, $stop = 5) { for ($i = $start; $i < $stop; $i++) { $res [] = self::$getLines [$i]; } return $res; unset ($res); } public static function setLines ($lines) { self::$getLines = (array)$lines; $this->getChange (self::$getLines, DB::PRESERVE); } public function &setLinesFromArray ($lines) { $LinesById [] = array(); foreach($lines as $line) { $LinesById [] = (array) $lines; } self::setLines ($LinesById); } public function &setLineByIdLine ($i, $replace) { self::$getLines [$i] = $replace; $this->getChange (implode ( "\n", self::$getLines), DB::PRESERVE); } } $db = new DB('file.txt'); $db -> getChange ('Hi Whats up its a test file', DB::PRESERVE); $db -> getChange ('Thats another one', DB::PRESERVE); $db -> getChange ('Once again Thats another one', DB::PRESERVE); //this will override contents of previously created file //$db -> getChange ('Once again Thats another one yo !'); // this will fetch array b/w required pos // print_r ( $db->getBetween (0, 3)); // this will change value of key num $db->setLineByIdLine (1 ,'Line One Changed'); // append array from position $db -> getAppendLine (1 ,array('Append','Append2')); echo "\n Count ".$db -> getCount () . "\n"; /* create file from array $db->setLineFromArray( array('New One', 'Another One', 'Thats Hide One', )); */ $db->getOutput ();