Source for file file.php
Documentation is available at file.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> |
// | Sebastian Bergmann <sb@sebastian-bergmann.de> |
// +----------------------------------------------------------------------+
// $Id: file.php,v 1.18 2006/01/31 13:40:00 bate Exp $
require_once 'Cache/Container.php';
* Stores cache contents in a file.
* @author Ulf Wendel <ulf.wendel@phpdoc.de>
* @version $Id: file.php,v 1.18 2006/01/31 13:40:00 bate Exp $
* With file container, it's possible, that you get corrupted
* data-entries under bad circumstances. The file locking must
* improve this problem but it's experimental stuff. So the
* default value is false. But it seems to give good results
* Directory where to put the cache files.
* @var string Make sure to add a trailing slash
* Filename prefix for cache files.
* You can use the filename prefix to implement a "domain" based cache or just
* to give the files a more descriptive name. The word "domain" is borroed from
* a user authentification system. One user id (cached dataset with the ID x)
* may exists in different domains (different filename prefix). You might want
* to use this to have different cache values for a production, development and
* quality assurance system. If you want the production cache not to be influenced
* by the quality assurance activities, use different filename prefixes for them.
* I personally don't think that you'll never need this, but 640kb happend to be
* not enough, so... you know what I mean. If you find a useful application of the
* feature please update this inline doc.
* List of cache entries, used within a gc run
* Total number of bytes required by all cache entries, used within a gc run.
* Max Line Length of userdata
* If set to 0, it will take the default
* ( 1024 in php 4.2, unlimited in php 4.3)
* see http://ch.php.net/manual/en/function.fgets.php
* Creates the cache directory if neccessary
* @param array Config options: ["cache_dir" => ..., "filename_prefix" => ...]
// make relative paths absolute for use in deconstructor.
// it looks like the deconstructor has problems with relative paths
// check if a trailing slash is in cache_dir
$this->group_dirs = array ();
function fetch($id, $group)
if (PEAR ::isError ($file)) {
return array (null , null , null );
if (!($fh = @fopen($file, 'rb'))) {
return new Cache_Error(" Can't access cache file '$file'. Check access rights and path." , __FILE__ , __LINE__ );
// File locking (shared lock)
// 1st line: expiration date
// 3rd+ lines: cache data
$buffer .= fread($fh, 8192 );
$cachedata = $this->decode($buffer);
// last usage date used by the gc - maxlifetime
// touch without second param produced stupid entries...
return array ($expire, $cachedata, $userdata);
* WARNING: If you supply userdata it must not contain any linebreaks,
* otherwise it will break the filestructure.
function save($id, $cachedata, $expires, $group, $userdata)
if (!($fh = @fopen($file, 'wb'))) {
return new Cache_Error(" Can't access '$file' to store cache data. Check access rights and path." , __FILE__ , __LINE__ );
// File locking (exclusive lock)
// 1st line: expiration date
// 3rd+ lines: cache data
fwrite($fh, $userdata . "\n");
// I'm not sure if we need this
// i don't think we need this (chregu)
if (PEAR ::isError ($file)) {
unset ($this->group_dirs [$group]);
* Deletes all expired files.
* Garbage collection for files is a rather "expensive", "long time"
* operation. All files in the cache directory have to be examined which
* means that they must be opened for reading, the expiration date has to be
* read from them and if neccessary they have to be unlinked (removed).
* If you have a user comment for a good default gc probability please add it to
* @param integer Maximum lifetime in seconds of an no longer used/touched entry
// check the space used by the cache entries
if (@unlink($entry['file'])) {
new CacheError (" Can't delete {$entry['file']}. Check the permissions." );
} // end func garbageCollection
* Does the recursive gc procedure, protected.
* @param integer Maximum lifetime in seconds of an no longer used/touched entry
* @param string directory to examine - don't sets this parameter, it's used for a
* recursive function call!
return new Cache_Error(" Can't remove directory '$dir'. Check permissions and path." , __FILE__ , __LINE__ );
if ('.' == $file || '..' == $file)
// skip trouble makers but inform the user
if (!($fh = @fopen($file, 'rb'))) {
new Cache_Error(" Can't access cache file '$file', skipping it. Check permissions and path." , __FILE__ , __LINE__ );
$expire = fgets($fh, 11 );
$this->entries[$lastused] = array ('file' => $file, 'size' => filesize($file));
if (( ($expire && $expire <= time()) || ($lastused <= (time() - $maxlifetime)) ) && !unlink($file)) {
new Cache_Error(" Can't unlink cache file '$file', skipping. Check permissions and path." , __FILE__ , __LINE__ );
// flush the disk state cache
} // end func doGarbageCollection
* Returns the filename for the specified id.
* @param string dataset ID
* @param string cache group
* @return string full filename with the path
if (isset ($this->group_dirs [$group])) {
return new Cache_Error(" Can't make directory '$dir'. Check permissions and path." , __FILE__ , __LINE__ );
$this->group_dirs [$group] = $dir;
} // end func getFilename
* Deletes a directory and all files in it.
* @param string directory
* @return integer number of removed files
return new Cache_Error(" Can't remove directory '$dir'. Check permissions and path." , __FILE__ , __LINE__ );
while (false !== $file = readdir($dh)) {
if ('.' == $file || '..' == $file)
// according to php-manual the following is needed for windows installations.
if ($dir != $this->cache_dir) { //delete the sub-dir entries itself also, but not the cache-dir.
Documentation generated on Mon, 11 Mar 2019 15:25:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|