2005-10-03 04:48:00 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience class for reading, writing and appending to files.
|
2005-12-27 03:33:44 +00:00
|
|
|
*
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
2006-01-20 07:46:14 +00:00
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
2005-12-23 21:57:26 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
2005-12-23 21:57:26 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
2005-12-27 03:33:44 +00:00
|
|
|
* @filesource
|
2006-01-20 07:46:14 +00:00
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
2005-12-23 21:57:26 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
2005-10-03 04:48:00 +00:00
|
|
|
* @package cake
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake.libs
|
2005-10-03 04:48:00 +00:00
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Included libraries.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
*/
|
2005-12-27 17:14:32 +00:00
|
|
|
if(!class_exists('Object'))
|
2005-12-27 03:33:44 +00:00
|
|
|
{
|
|
|
|
uses('object');
|
|
|
|
}
|
2006-02-08 15:25:34 +00:00
|
|
|
if(!class_exists('Folder'))
|
|
|
|
{
|
|
|
|
uses('folder');
|
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience class for reading, writing and appending to files.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @package cake
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake.libs
|
2005-10-03 04:48:00 +00:00
|
|
|
* @since CakePHP v 0.2.9
|
|
|
|
*/
|
|
|
|
class File extends Object
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Folder of the File
|
|
|
|
*
|
|
|
|
* @var Folder
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $folder = null;
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filename
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
var $name = null;
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param boolean $create Create file if it does not exist
|
|
|
|
* @return File
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function __construct ($path, $create = false)
|
|
|
|
{
|
2005-12-24 07:56:48 +00:00
|
|
|
parent::__construct();
|
2005-10-03 04:48:00 +00:00
|
|
|
|
2005-12-24 07:56:48 +00:00
|
|
|
$this->folder = new Folder(dirname($path), $create);
|
|
|
|
$this->name = basename($path);
|
2005-10-03 04:48:00 +00:00
|
|
|
|
2005-12-24 07:56:48 +00:00
|
|
|
if (!$this->exists())
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
2005-12-24 07:56:48 +00:00
|
|
|
if ($create === true)
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
2005-12-24 07:56:48 +00:00
|
|
|
if (!$this->create())
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
2006-02-18 23:42:21 +00:00
|
|
|
return false;
|
2005-10-03 04:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the contents of this File as a string.
|
|
|
|
*
|
|
|
|
* @return string Contents
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function read ()
|
|
|
|
{
|
2005-12-24 07:56:48 +00:00
|
|
|
return file_get_contents($this->getFullPath());
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append given data string to this File.
|
|
|
|
*
|
|
|
|
* @param string $data Data to write
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function append ($data)
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return $this->write($data, 'a');
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-27 03:33:44 +00:00
|
|
|
* Write given data to this File.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
2005-12-27 03:33:44 +00:00
|
|
|
* @param string $data Data to write to this File.
|
|
|
|
* @param string $mode Mode of writing. {@link http://php.net/fwrite See fwrite()}.
|
2005-10-03 04:48:00 +00:00
|
|
|
* @return boolean Success
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function write ($data, $mode = 'w')
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
$file = $this->getFullPath();
|
|
|
|
if (!($handle = fopen( $file , $mode)))
|
|
|
|
{
|
|
|
|
print ("[File] Could not open $file with mode $mode!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fwrite($handle, $data))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!fclose($handle))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get md5 Checksum of file with previous check of Filesize
|
|
|
|
*
|
2005-12-27 03:33:44 +00:00
|
|
|
* @param string $force Data to write to this File.
|
2005-10-03 04:48:00 +00:00
|
|
|
* @return string md5 Checksum {@link http://php.net/md5_file See md5_file()}
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function getMd5 ($force = false)
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
$md5 = '';
|
|
|
|
if ( $force == true || $this->getSize(false) < MAX_MD5SIZE )
|
|
|
|
{
|
|
|
|
$md5 = md5_file( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $md5;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the Filesize, either in bytes or in human-readable format.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
2005-12-27 03:33:44 +00:00
|
|
|
* @param boolean $humanReadeble Data to write to this File.
|
2005-10-18 22:27:39 +00:00
|
|
|
* @return string|int filesize as int or as a human-readable string
|
2005-10-03 04:48:00 +00:00
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function getSize ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
$size = filesize( $this->getFullPath() );
|
|
|
|
return $size;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the File extension.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return string The Fileextension
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function getExt ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
$ext = '';
|
|
|
|
|
|
|
|
$parts = explode('.', $this->getName() );
|
|
|
|
|
|
|
|
if ( count($parts) > 1 )
|
|
|
|
{
|
|
|
|
$ext = array_pop( $parts );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$ext = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ext;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the filename.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return string The Filename
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function getName ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return $this->name;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the File's owner.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return int the Fileowner
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function getOwner ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return fileowner( $this->getFullPath() );
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the File group.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return int the Filegroup
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function getGroup ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return filegroup( $this->getFullPath() );
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Creates the File.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
2005-10-18 22:27:39 +00:00
|
|
|
* @return boolean Success
|
2005-10-03 04:48:00 +00:00
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function create ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
$dir = $this->folder->pwd();
|
|
|
|
if ( file_exists( $dir ) && is_dir($dir) && is_writable($dir) && !$this->exists() )
|
|
|
|
{
|
|
|
|
if ( !touch( $this->getFullPath() ) )
|
|
|
|
{
|
|
|
|
print ("[File] Could not create $this->getName()!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print ("[File] Could not create $this->getName()!");
|
|
|
|
return false;
|
|
|
|
}
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns true if the File exists.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function exists ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return file_exists( $this->getFullPath() );
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Deletes the File.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function delete ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return unlink( $this->getFullPath() );
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns true if the File is writable.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function writable ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return is_writable( $this->getFullPath() );
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns true if the File is executable.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function executable ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return is_executable( $this->getFullPath() );
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns true if the File is readable.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function readable ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return is_readable( $this->getFullPath() );
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns last access time.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return int timestamp
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function lastAccess ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return fileatime( $this->getFullPath() );
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns last modified time.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return int timestamp
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function lastChange ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return filemtime( $this->getFullPath() );
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the current folder.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return Folder
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function getFolder ()
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return $this->folder;
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the "chmod" (permissions) of the File.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function getChmod ( )
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return substr(sprintf('%o', fileperms($this->getFullPath())), -4);
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the full path of the File.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2006-02-18 23:42:21 +00:00
|
|
|
function getFullPath ( )
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
return Folder::slashTerm($this->folder->pwd()).$this->getName();
|
2006-02-18 23:42:21 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
}
|
|
|
|
|
2005-12-27 03:33:44 +00:00
|
|
|
?>
|