PHP
  Home arrow PHP arrow Page 3 - PHP Datastorage Class
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

PHP Datastorage Class
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2005-11-14


    Table of Contents:
  • PHP Datastorage Class
  • Top of the Page
  • Flat Files
  • Getting Data In
  • Getting Data Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    PHP Datastorage Class - Flat Files
    ( Page 3 of 5 )

    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
     

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek