Location: PHPKode > scripts > fileStorage > filestorage/fileStorage.php
<?php
/*****************************************************************************
 * fileStorage: easy to use class for writing and reading serialized data from
 *              and to files
 *****************************************************************************
 * version 1.0 - 2002-09-12 12:15
 *****************************************************************************
 * Copyright (c) 2002, Emilis Dambauskas, hide@address.com
 * All rights reserved.
 * This class is licensed under The Artistic license, for more info read 
 * fileStorage.license.txt or go to 
 * http://www.opensource.org/licenses/artistic-license.php
 ****************************************************************************/

/*****************************************************************************
 * HELP:
 * methods:
 * - fileStorage($filename) - contructor
 * - error($msg) - reports an error
 * - read_data() - reads data from file
 * - write_data() - writes data to file
 * - add_data($name, $value) - adds new value into data storage
 * - get_data($name) - gets value from data storage
 * - register_data($name, &$value) - adds reference into data storage
 * - &get_reg_data($name) - gets reference from data storage
 *
 * ATTENTION:
 *   if you want to store data in a file it has to be accessible for writing
 *   by the web server. So you may need to chmod the file and change its
 *   access permissions.
 ****************************************************************************/

/*****************************************************************************
 * EXAMPLE (a simple counter stored in the storage):
 * 
 * // we create new storage and associate it with file mydata.dat:
 * $storage = &new fileStorage('mydata.dat');
 * $storage->read_data();
 * 
 * // try to get counter value from storage
 * $counter = $storage->get_data('counter');
 * 
 * // if counter not found:
 * if (!$counter)
 *    $counter = 1;
 * 
 * // increase counter value:
 * $counter++;
 *
 * // store counter value in the storage:
 * $storage->add_data('counter', $counter)
 * // save storage:
 * $storage->write_data();
 * 
 * 
 ****************************************************************************/

class fileStorage
{
	var $fp;
	var $filename;
	var $data;
	var $data_read;
	
	/**
	 * constructor
	 *
	 * $filename = name of file to use for storing data
	 */
	function fileStorage($filename)
	{
		$this->filename = $filename;
		$this->data_read = FALSE;
		$this->data = array();
	} // end of function fileStore
	
	/**
	 * reports an error
	 * tip: you ca attach your own error reporting here
	 */
	function error($msg)
	{
		trigger_error('fileStorage error: '.$msg);
	} // end of function error
	
	
	/**
	 * checks if file exists
	 * if not tries to create it
	 */
	function _check_file()
	{
		if (!file_exists($this->filename))
				if(!$this->fp = fopen($this->filename, 'w'))
						return FALSE;
				else
						fclose($this->fp);
		return TRUE;
	} // end of function _check_file
	
	/**
	 * reads the file and returns array with data
	 */
	function read_data()
	{
		if (!$this->_check_file())
		{
			$this->error('Couldnot open file '.$this->filename.' for reading.');
			return FALSE;
		}
				
		$this->fp = fopen ($this->filename, 'r');
		$contents = fread ($this->fp, filesize ($this->filename));
		fclose ($this->fp); 
		$this->data = unserialize($contents);
		$this->data_read = TRUE;
		
		return $this->data;
	} // end of function get_data
	
	/**
	 * writes data into the file
	 */
	function write_data()
	{
		if (!$this->_check_file())
		{
			$this->error('Couldnot open file '.$this->filename.' for writing.');
			return FALSE;
		}
		
		$store = serialize($this->data);
		$this->fp = fopen($this->filename, 'w');
		fwrite($this->fp, $store);
		fclose($this->fp);
	} // end of function write_data
	
	
	/**
	 * adds variable into storage
	 */
	function add_data($name, $value)
	{
		if (!$this->data_read)
				$this->read_data();
		
		$this->data[(string) $name] = $value;
	} // end of function add_data
	
	/**
	 * returns data stored in storage by that name
	 */
	function get_data($name)
	{
		if (!$this->data_read)
				$this->read_data();
		
		if (!isset($this->data[(string)$name]))
		{
			$this->error('Data '.$name.' does not exist in the storage');
			return FALSE;
		}
		return $this->data[(string)$name];
	} // end of function get_data
	
	/**
	 *
	 */
	function register_data($name, &$value)
	{
		if (!$this->data_read)
				$this->read_data();
		
		$this->data[(string) $name] = &$value;
	}
	
	/**
	 *
	 */
	function &get_reg_data($name)
	{
		if (!$this->data_read)
				$this->read_data();
		
		if (!isset($this->data[(string)$name]))
		{
			$this->error('Data '.$name.' does not exist in the storage');
			return FALSE;
		}
		return $this->data[(string)$name];
	}

} // end of class fileStorage

?>
Return current item: fileStorage