* @author Tal Peer * @author Michael Wallner * @copyright 2002-2005 The Authors * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id$ * @link http://pear.php.net/package/File */ /** * Requires PEAR */ require_once 'PEAR.php'; /** * The default number of bytes for reading */ if (!defined('FILE_DEFAULT_READSIZE')) { define('FILE_DEFAULT_READSIZE', 1024, true); } /** * The maximum number of bytes for reading lines */ if (!defined('FILE_MAX_LINE_READSIZE')) { define('FILE_MAX_LINE_READSIZE', 40960, true); } /** * Whether file locks should block */ if (!defined('FILE_LOCKS_BLOCK')) { define('FILE_LOCKS_BLOCK', true, true); } /** * Mode to use for reading from files */ define('FILE_MODE_READ', 'rb', true); /** * Mode to use for truncating files, then writing */ define('FILE_MODE_WRITE', 'wb', true); /** * Mode to use for appending to files */ define('FILE_MODE_APPEND', 'ab', true); /** * Use this when a shared (read) lock is required */ define('FILE_LOCK_SHARED', LOCK_SH | (FILE_LOCKS_BLOCK ? 0 : LOCK_NB), true); /** * Use this when an exclusive (write) lock is required */ define('FILE_LOCK_EXCLUSIVE', LOCK_EX | (FILE_LOCKS_BLOCK ? 0 : LOCK_NB), true); /** * Class for handling files * * A class with common functions for writing, * reading and handling files and directories * * @author Richard Heyes * @author Tal Peer * @author Michael Wallner * @access public * @package File * * @static */ class File extends PEAR { /** * Destructor * * Unlocks any locked file pointers and closes all filepointers * * @access private */ function _File() { File::closeAll(); } /** * Handles file pointers. If a file pointer needs to be opened, * it will be. If it already exists (based on filename and mode) * then the existing one will be returned. * * @access private * @param string $filename Filename to be used * @param string $mode Mode to open the file in * @param mixed $lock Type of lock to use * @return mixed PEAR_Error on error or file pointer resource on success */ function _getFilePointer($filename, $mode, $lock = false) { $filePointers = &PEAR::getStaticProperty('File', 'filePointers'); // Win32 is case-insensitive if (OS_WINDOWS) { $filename = strtolower($filename); } // check if file pointer already exists if (!isset($filePointers[$filename][$mode]) || !is_resource($filePointers[$filename][$mode])) { // check if we can open the file in the desired mode switch ($mode) { case FILE_MODE_READ: if (!preg_match('/^.+(? $modes) { foreach (array_keys($modes) as $mode) { if (is_resource($filePointers[$fname][$mode])) { @fclose($filePointers[$fname][$mode]); } unset($filePointers[$fname][$mode]); } } } } /** * This closes an open file pointer * * @access public * @param string $filename The filename that was opened * @param string $mode Mode the file was opened in * @return mixed PEAR Error on error, true otherwise */ function close($filename, $mode) { $filePointers = &PEAR::getStaticProperty('File', 'filePointers'); if (OS_WINDOWS) { $filename = strToLower($filename); } if (!isset($filePointers[$filename][$mode])) { return true; } $fp = $filePointers[$filename][$mode]; unset($filePointers[$filename][$mode]); if (is_resource($fp)) { // unlock file @flock($fp, LOCK_UN); // close file if (!@fclose($fp)) { return PEAR::raiseError("Cannot close file: $filename"); } } return true; } /** * This unlocks a locked file pointer. * * @access public * @param string $filename The filename that was opened * @param string $mode Mode the file was opened in * @return mixed PEAR Error on error, true otherwise */ function unlock($filename, $mode) { $fp = File::_getFilePointer($filename, $mode); if (PEAR::isError($fp)) { return $fp; } if (!@flock($fp, LOCK_UN)) { return PEAR::raiseError("Cacnnot unlock file: $filename"); } return true; } /** * @deprecated */ function stripTrailingSeparators($path, $separator = DIRECTORY_SEPARATOR) { if ($path === $separator) { return $path; } return rtrim($path, $separator); } /** * @deprecated */ function stripLeadingSeparators($path, $separator = DIRECTORY_SEPARATOR) { if ($path === $separator) { return $path; } return ltrim($path, $separator); } /** * @deprecated Use File_Util::buildPath() instead. */ function buildPath($parts, $separator = DIRECTORY_SEPARATOR) { require_once 'File/Util.php'; return File_Util::buildPath($parts, $separator); } /** * @deprecated Use File_Util::skipRoot() instead. */ function skipRoot($path) { require_once 'File/Util.php'; return File_Util::skipRoot($path); } /** * @deprecated Use File_Util::tmpDir() instead. */ function getTempDir() { require_once 'File/Util.php'; return File_Util::tmpDir(); } /** * @deprecated Use File_Util::tmpFile() instead. */ function getTempFile($dirname = null) { require_once 'File/Util.php'; return File_Util::tmpFile($dirname); } /** * @deprecated Use File_Util::isAbsolute() instead. */ function isAbsolute($path) { require_once 'File/Util.php'; return File_Util::isAbsolute($path); } /** * @deprecated Use File_Util::relativePath() instead. */ function relativePath($path, $root, $separator = DIRECTORY_SEPARATOR) { require_once 'File/Util.php'; return File_Util::relativePath($path, $root, $separator); } /** * @deprecated Use File_Util::realpath() instead. */ function realpath($path, $separator = DIRECTORY_SEPARATOR) { require_once 'File/Util.php'; return File_Util::realpath($path, $separator); } } PEAR::registerShutdownFunc(array('File', '_File')); ?>