Home arrow PHP arrow Page 3 - PHP Datastorage Class

Flat Files - PHP

Many websites use databases for storing data for use by web applications. There are situations in which this is not the best solution, however. For light, moderate, or temporary storage requirements, you might prefer to use flat files, session variables, and cookies. This article will describe how to create a class that handle all of these. It is the first of two parts.

TABLE OF CONTENTS:
  1. PHP Datastorage Class
  2. Top of the Page
  3. Flat Files
  4. Getting Data In
  5. Getting Data Out
By: Chris Root
Rating: starstarstarstarstar / 10
November 14, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If the mode is set to "f" then we will be storing the data in a flat file. All the usual permissions issues and security precautions are in force here, and the file may not be created if they aren't followed.

else if($mode == "f")
{
     if($dir == "")
     {
           $this->data_dir = $this->self."/data";
     }
     else
     {
           $this->data_dir = $this->self."/".$dir;
     }
     if(!is_dir($this->data_dir))
     {
           if(!mkdir($this->data_dir))
           {
                $this->err("Trouble making directory",1);
           }
     }
     if(!file_exists($this->data_dir.$this->dataname.".dta"))
     {
           if(!$this->datafile = fopen($this->data_dir.$this->dataname.".dta","w"))
           {
                $this->err("Problem Creating Datafile",1);/**/
           }
           else
           {
                fclose($this->datafile);
           }
     }
     else
     {
           if(!$datastr = file_get_contents($this->data_dir.$this->dataname.".dta"))
           {
                $this->err("Problem Opening Datafile",1);
           }
           else
           {
                $this->items = unserialize($datastr);
           }
     }
}

You may have noticed that I haven't mentioned the third and final argument to the constructor. Until now it didn't have any purpose; now it does.

The dir argument sets an optional directory for the files that datastore uses. The various if/else blocks in this code take care of determining whether the default directory or a user specified one should be used and creating that directory if it doesn't yet exist. The default directory is called "data" and resides in the same directory as the datastore class regardless of the file that includes it. This is accomplished using the magic constant "__FILE__". This built-in PHP constant returns the path to the file, unlike $PHP_SELF which always refers to the master page. Any includes lose their selfness when they are included by another file.

The cookie code is something we will deal with later. For now, this is the constructor method in its entirety.



 
 
>>> More PHP Articles          >>> More By Chris Root
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: