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/>
|
2005-12-27 03:33:44 +00:00
|
|
|
* Copyright (c) 2005, 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
|
2005-12-23 21:57:26 +00:00
|
|
|
* @copyright Copyright (c) 2005, Cake Software Foundation, Inc.
|
|
|
|
* @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');
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
var $folder = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filename
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $name = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param boolean $create Create file if it does not exist
|
|
|
|
* @return File
|
|
|
|
*/
|
2005-12-24 07:56:48 +00:00
|
|
|
function __construct ($path, $create = false)
|
2005-10-03 04:48:00 +00:00
|
|
|
{
|
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
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the contents of this File as a string.
|
|
|
|
*
|
|
|
|
* @return string Contents
|
|
|
|
*/
|
|
|
|
function read ()
|
|
|
|
{
|
2005-12-24 07:56:48 +00:00
|
|
|
return file_get_contents($this->getFullPath());
|
2005-10-03 04:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append given data string to this File.
|
|
|
|
*
|
|
|
|
* @param string $data Data to write
|
|
|
|
* @return boolean Success
|
|
|
|
*/
|
|
|
|
function append ($data)
|
|
|
|
{
|
|
|
|
return $this->write($data, 'a');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
function write ($data, $mode = 'w')
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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()}
|
|
|
|
*/
|
|
|
|
function getMd5 ($force = false)
|
|
|
|
{
|
|
|
|
$md5 = '';
|
|
|
|
if ( $force == true || $this->getSize(false) < MAX_MD5SIZE )
|
|
|
|
{
|
|
|
|
$md5 = md5_file( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $md5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
|
|
|
function getSize ()
|
|
|
|
{
|
|
|
|
$size = filesize( $this->getFullPath() );
|
|
|
|
return $size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the File extension.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return string The Fileextension
|
|
|
|
*/
|
|
|
|
function getExt ()
|
|
|
|
{
|
|
|
|
$ext = '';
|
|
|
|
|
|
|
|
$parts = explode('.', $this->getName() );
|
|
|
|
|
|
|
|
if ( count($parts) > 1 )
|
|
|
|
{
|
|
|
|
$ext = array_pop( $parts );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$ext = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the filename.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return string The Filename
|
|
|
|
*/
|
|
|
|
function getName ()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
function getOwner ()
|
|
|
|
{
|
|
|
|
return fileowner( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the File group.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return int the Filegroup
|
|
|
|
*/
|
|
|
|
function getGroup ()
|
|
|
|
{
|
|
|
|
return filegroup( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
|
|
|
function create ()
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns true if the File exists.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function exists ()
|
|
|
|
{
|
|
|
|
return file_exists( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Deletes the File.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function delete ()
|
|
|
|
{
|
|
|
|
return unlink( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
function writable ()
|
|
|
|
{
|
|
|
|
return is_writable( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
function executable ()
|
|
|
|
{
|
|
|
|
return is_executable( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
function readable ()
|
|
|
|
{
|
|
|
|
return is_readable( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns last access time.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return int timestamp
|
|
|
|
*/
|
|
|
|
function lastAccess ()
|
|
|
|
{
|
|
|
|
return fileatime( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns last modified time.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return int timestamp
|
|
|
|
*/
|
|
|
|
function lastChange ()
|
|
|
|
{
|
|
|
|
return filemtime( $this->getFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-18 22:27:39 +00:00
|
|
|
* Returns the current folder.
|
2005-10-03 04:48:00 +00:00
|
|
|
*
|
|
|
|
* @return Folder
|
|
|
|
*/
|
|
|
|
function getFolder ()
|
|
|
|
{
|
|
|
|
return $this->folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
function getChmod ( )
|
|
|
|
{
|
|
|
|
return substr(sprintf('%o', fileperms($this->getFullPath())), -4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
|
|
|
function getFullPath ( )
|
|
|
|
{
|
|
|
|
return Folder::slashTerm($this->folder->pwd()).$this->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-27 03:33:44 +00:00
|
|
|
?>
|