Please login or register. Welcome to the Studio, guest!

Quick Links:



Chris Avatar

Head Coder


June 2005
class cache {
// Cache class. Whee.
var $cache_folder = false;
var $file_list = Array();
var $file_list_updated = false;

function cache($folder_name){
// Starts the creation of the cache
// $folder_name is the name of the cache folder.

if(is_dir($folder_name)) // Make sure the directory exists.
$this->cache_folder = $folder_name;
else if(is_dir("/".$folder_name)) // Check for a slash in front of the name also.
$this->cache_folder = "/".$folder_name;

function listFiles(){
// Returns an array that contains a list of all files in the folder (ignoring the . and ..)

if($this->cache_folder === false) // Make sure the folder exists
return Array("Folder does not exist.");

if($this->file_list_updated == true) // Check to see if the list is already up to date
return $this->file_list;

// Delete the previous list and create a new one
$this->file_list = Array();

// Grab the directory and scan
$x = dir($this->cache_folder);
while(($d = $x->read()) !== false){
if($d == "." || $d == "..") // Make sure the file isn't referring to a directory
$this->file_list[] = $d; // Add the file to the list

//  Set the lsit as updated and then return the list
$this->file_list_updated = true;
return $this->file_list;

function getFile($file_name){
// Returns the content of a file in the cache
// $file_name is the name of the file from who to get the contents

if($this->cache_folder === false) // Make sure folder exists
return false;

if(is_file($this->cache_folder."/".$file_name)) // Make sure the file exists. Return false otherwise
return file_get_contents($this->cache_folder."/".$file_name);

return false;

function updateFile($file_name,$content,$add=false){
// This will update the contents of the specified file. Creates the file if the file doesn't exist.
// $file_name is the name of the file to be updated.
// $content is the content to be put into the file.
// $add is whether to add to the contents of the file or to override them. true = add, false = override.

if($this->cache_folder === false)
return false; // Return false to say the file was not updated

if($add === true)
$type = "a"; // Adds to the end of file
$type = "w"; // Clears file then adds

$fp = fopen($this->cache_folder."/".$file_name,$type);

$this->file_list_updated = false;
return true;

function deleteFile($file_name){
// Deletes the specified file.
// $file_name is the name of the file to be deleted.

if($this->cache_folder === false)
return false;

return true;

function timeFile($file_name){
// Returns the time of the last updated to the file. Returns 0 if file doesn't exist.
// $file_name is the name of the file

if($this->cache_folder === false)
return 0; // Return 0 on an error

if(is_file($this->cache_folder."/".$file_name)) // Make sure file exists
return filemtime($this->cache_folder."/".$file_name);

return 0; // Return 0 if file doesn't exist

$c = new cache("folder");

This is something I whipped together not too long ago (as in, 10-20 minutes ago). In essence, it allows you to manage a cache. However, it could also be used for a simple flat-file system with the exception of creating folders (because those shouldn't be needed with a cache).