flatfile DB class(suggestion)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    flatfile DB class(suggestion)

    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 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($fhLOCK_EX))
    {
    fwrite($fh$write_data);
    flock($fhLOCK_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($fhLOCK_EX))
    {
    fwrite($fh$put_data);
    flock($fhLOCK_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(0FALSE); // 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
    Last edited by softwarefreak; 04.03.12, 18:54.
    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

    #2
    Originally posted by softwarefreak View Post
    //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!

    Comment


      #3
      Replace file_get_contents with readfile as readfile is faster then fgs

      Comment

      Working...
      X