the Future of the Web
  • Home
  • Hire Us
  • Articles
  • About
  • Contact
  • Saving data to a file with PHP

    Feb 24 2008

    Lately, I've been skipping using MySQL in situations where I just want to store a few variables, like configuration options, and don't necessarily want the hassle of setting up a database.

    You can easily store data to a file using serialize and unserialize to turn a PHP object into a string, and then read and write the string in a file.

    Here are a few functions that do just that:

    function get_data($filename) {
        // create file if it doesn't exist
        if (!file_exists($filename)) {
            touch($filename);
        }
    
        return unserialize(file_get_contents($filename));
    }
    
    function get_option($filename, $key) {
        $data = get_data($filename);
        return $data[$key];
    }
    
    function set_option($filename, $key, $value) {
        $data = get_data($filename);
        $data[$key] = $value;
    
        // write to disk
        $fp = fopen($filename, 'w');
        fwrite($fp, serialize($data));
        fclose($fp);
    }
    
    // probably should put somewhere off the web root
    $config = '../config.dat';
    
    set_option($config, 'width', 1024);
    echo get_option($config, 'width'); // will echo 1024
    

    So there you have it. Feel free to use or modify this code as much as you like. If anyone has an idea for rewriting it to be cleaner, please share in the comments.

    Tags: php
    View 5 Comments | Add a comment
  • Comments

    1. Ed Eliot at 12:03am on February 25, 2008

    It might be an idea to cache reading of options and buffer writing. That way you won't potentially hit the file multiple times on a single request.

    I'd suggest wrapping the functions in a class to make this a bit easier to achieve.

    2. Harmen Janssen at 12:53am on February 25, 2008

    Make sure to familiarize yourself with the __sleep and __wakeup methods when serializing objects (http://php.net/__sleep)!

    Also, for an object oriented solution, you might be interested in the File and Cache classes I've written (http://whatphp.net/class.php?package=util/FileSystem/File/1.0&class=File&view=api and http://whatphp.net/class.php?class=Cache&package=util/Cache/1.0)

    3. Handan Daily at 5:16am on March 26, 2008

    jquery is great!

    4. Handan Daily at 5:17am on March 26, 2008

    your article chinese version
    http://www.ibm.com/developerworks/cn/xml/x-ajaxjquery.html

    呵呵!

    5. Someone at 3:29am on May 8, 2008

    Pretty good idea, I'll have plenty of uses for it.

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • Hire Me
    • About Me
    • Email Me
    • RSS Feed RSS Icon
  • Recent Articles

    • Parse Accept-Language to detect a user's language
    • Twitter
    • Three years of The Future of the Web
    • Saving data to a file with PHP
    • Easy web scraping with PHP
    • See all the articles
    • IBM: Where and when to use Ajax
    • Code Igniter 1.6.0 Released
    • Update a Dev Site Automatically with Subversion
    • JavaScript Functions are Variables
    • See All...
  • Categories

    • javascript (37)
    • links (19)
    • about (18)
    • web (14)
    • server (10)
    • html (10)
    • css (8)
    • carnival (7)
    • browsers (7)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
    • work (4)
  • Older Articles

    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2008 Jesse Skinner | CSS | XHTML | RSS | Help | Impressum | Cutie Quilts | Internet Blog Top Sites