Hi guys,especially arnage,something else,just_m3,master_v4, I was making this flatfile script not complete yet,
Can you please find a scope of improvement here in this code so dat It in real life works
You'll have to run this script to check how it works,
BTW, i tried to work w/ fseek() to edit in btw lines,but it gets complicated as to me, & this script can't withstand high load, I've seen this by holding the refresh button in my broswer,but I can be overcomed by using sleep(); so dat atleast the script waits 1-2 secs b4 writing in to the file, & flock will prevent from damaging the file,that's all from my side
Can you please find a scope of improvement here in this code so dat It in real life works
PHP Code:
<?php
class flatfile
{
/*
* Database file name
*/
private $database = "DB.php"; //I'm using a php file to hide the data in comments,which is not possible if we use a dat or text file
/*start comment text
*/
private $start_comment = "<?php\n/*\n";
/*
* end comment text
*/
private $end_comment = "\n*/\n?>";
/*
*
*/
public function write_data($text)
{
$data = $this->load_data();
$write_data = $this->start_comment.$data.$text."\n".$this->end_comment;
$fh = fopen($this->database, 'w');
if(flock($fh, LOCK_EX))
{
fwrite($fh, $write_data);
flock($fh, LOCK_UN);
}
fclose($fh);
return "";
}
/*
*
*/
private function load_data()
{
$file = file_get_contents($this->database);
$file = str_replace($this->start_comment, "", $file);
$file = str_replace($this->end_comment, "", $file);
return $file;
}
/*
*
*/
public function count_data()
{
$file = file($this->database);
$count = count($file);
return ($count-6);
}
public function return_data($start, $stop)
{
$file = file($this->database);
$count = count($file);
$j = ($start+2);
if($stop == FALSE || $stop > ($count-3))
{
$limit = ($count-3);
}
else
{
$limit = $stop+3;
}
for ($i=$j; $i < $limit; $i++)
{
$data[] = $file[$i];
}
return $data;
}
public function edit_data($line_no, $replace)
{
$data = $this->load_data();
$chunk = explode("\n", $data);
$chunk[$line_no] = str_replace($chunk[$line_no], $replace, $chunk[$line_no]);
$text = implode("\n", $chunk);
$put_data = $this->start_comment.$text.$this->end_comment;
$fh = fopen($this->database, 'w');
if(flock($fh, LOCK_EX))
{
fwrite($fh, $put_data);
flock($fh, LOCK_UN);
}
fclose($fh);
}
/*
* End of class
*/
}
/*
*
* Create an instance of the class
*/
$db = new flatfile;
//to write data in the file,we can simple use the write_data method
$text = "This is a comment";
$db->write_data($text);
//to fetch the content in the file
$data = $db->return_data(0, FALSE); // false will work like,select * from
/*we can also limit it like
*
*
* $data = $db->return_data(0, 3);
* print_r($data);
*/
print_r($data);
/*Now we can edit a line with this method
*
*
*/
$edit = "This is an edited comment";
$db->edit_data(0, $edit); // the first param is the line number, 0 here
print_r($data);
/*
*
* free up the memory taken by the object
*/
unset($db);
?>
BTW, i tried to work w/ fseek() to edit in btw lines,but it gets complicated as to me, & this script can't withstand high load, I've seen this by holding the refresh button in my broswer,but I can be overcomed by using sleep(); so dat atleast the script waits 1-2 secs b4 writing in to the file, & flock will prevent from damaging the file,that's all from my side
Comment