Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 24th, 2012  |  syntax: None  |  size: 0.65 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3.  * Datastore class
  4.  * for working with flat file data.
  5.  * */
  6. class Datastore {
  7.        
  8.         public function __construct() {
  9.         }
  10.        
  11.         public function read_file( $file ) {
  12.                 $file = './' . $file;
  13.                 if ( !file_exists( $file ) ) {
  14.                         return false;
  15.                 }
  16.                 return file_get_contents( $file );
  17.         }
  18.  
  19.         public function save_file($relativepath, $contents, $append = false ) {
  20.                 $parts = explode('/', $relativepath);
  21.                 $file = array_pop($parts);
  22.                 $dir = '.';
  23.                 foreach($parts as $part) {
  24.                         if(!is_dir($dir .= '/' . $part)) {
  25.                                 $oldumask = umask(0);
  26.                                 mkdir($dir, 0777);
  27.                                 umask($oldumask);
  28.                         }
  29.                 }
  30.                 file_put_contents($dir . '/' . $file, $contents, ($append?FILE_APPEND:0);
  31.         }
  32.  
  33. }