flatfile DB class(suggestion)

Started on the old forum in March 2012 · view the original in the archive · old links redirect here

2 replies · 373 views · started 14 years ago · 1 min read

3 participants
softwarefreak Senior · 349 posts #1

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

<?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);
?>

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

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

just_m3. Developer · 199 posts #2

softwarefreak wrote: //I'm using a php file to hide the data in comments,which is not possible if we use a dat or text file

here a tip .. try and set mime type application/x-httpd-php in .htaccess or server cPanel/mimetype for .dat extension or other . . . and then try to use 🙂 it might work

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

shushant Developer · 207 posts #3

Replace file_get_contents with readfile as readfile is faster then fgs

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close