Skip to content

Instantly share code, notes, and snippets.

@rlemon
Created February 24, 2012 15:57
Show Gist options
  • Save rlemon/1901754 to your computer and use it in GitHub Desktop.
Save rlemon/1901754 to your computer and use it in GitHub Desktop.
<?php
/*
* Datastore class
* for working with flat file data.
* */
class Datastore {
public function __construct() {
}
public function read_file( $file ) {
$file = './' . $file;
if ( !file_exists( $file ) ) {
return false;
}
return file_get_contents( $file );
}
public function save_file($relativepath, $contents, $append = false ) {
$parts = explode('/', $relativepath);
$file = array_pop($parts);
$dir = '.';
foreach($parts as $part) {
if(!is_dir($dir .= '/' . $part)) {
$oldumask = umask(0);
mkdir($dir, 0777);
umask($oldumask);
}
}
file_put_contents($dir . '/' . $file, $contents, ($append?FILE_APPEND:0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment