file.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: file_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * Convenience class for reading, writing and appending to files.
00005  *
00006  * PHP versions 4 and 5
00007  *
00008  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00009  * Copyright 2005-2008, Cake Software Foundation, Inc.
00010  *                              1785 E. Sahara Avenue, Suite 490-204
00011  *                              Las Vegas, Nevada 89104
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00018  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package         cake
00020  * @subpackage      cake.cake.libs
00021  * @since           CakePHP(tm) v 0.2.9
00022  * @version         $Revision: 675 $
00023  * @modifiedby      $LastChangedBy: gwoo $
00024  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00025  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * Included libraries.
00029  *
00030  */
00031     if (!class_exists('Object')) {
00032          uses ('object');
00033     }
00034 
00035     if (!class_exists('Folder')) {
00036          uses ('folder');
00037     }
00038 /**
00039  * Convenience class for reading, writing and appending to files.
00040  *
00041  * @package     cake
00042  * @subpackage  cake.cake.libs
00043  */
00044 class File extends Object{
00045 /**
00046  * Folder of the File
00047  *
00048  * @var Folder
00049  */
00050     var $folder = null;
00051 /**
00052  * Filename
00053  *
00054  * @var string
00055  */
00056     var $name = null;
00057 /**
00058  * Constructor
00059  *
00060  * @param string $path
00061  * @param boolean $create Create file if it does not exist
00062  * @return File
00063  */
00064     function __construct($path, $create = false) {
00065         parent::__construct();
00066         $this->folder = new Folder(dirname($path), $create);
00067         $this->name = basename($path);
00068         if (!$this->exists()) {
00069             if ($create === true) {
00070                 if (!$this->create()) {
00071                     return false;
00072                 }
00073             } else {
00074                 return false;
00075             }
00076         }
00077     }
00078 /**
00079  * Return the contents of this File as a string.
00080  *
00081  * @return string Contents
00082  */
00083     function read() {
00084         $contents = file_get_contents($this->getFullPath());
00085         return $contents;
00086     }
00087 /**
00088  * Append given data string to this File.
00089  *
00090  * @param string $data Data to write
00091  * @return boolean Success
00092  */
00093     function append($data) {
00094         return $this->write($data, 'a');
00095     }
00096 /**
00097  * Write given data to this File.
00098  *
00099  * @param string $data  Data to write to this File.
00100  * @param string $mode  Mode of writing. {@link http://php.net/fwrite See fwrite()}.
00101  * @return boolean Success
00102  */
00103     function write($data, $mode = 'w') {
00104         $file = $this->getFullPath();
00105         if (!($handle = fopen($file, $mode))) {
00106             print ("[File] Could not open $file with mode $mode!");
00107             return false;
00108         }
00109 
00110         if (!fwrite($handle, $data)) {
00111             return false;
00112         }
00113 
00114         if (!fclose($handle)) {
00115             return false;
00116         }
00117         return true;
00118     }
00119 /**
00120  * Get md5 Checksum of file with previous check of Filesize
00121  *
00122  * @param string $force Data to write to this File.
00123  * @return string md5 Checksum {@link http://php.net/md5_file See md5_file()}
00124  */
00125     function getMd5($force = false) {
00126         $md5 = '';
00127         if ($force == true || $this->getSize(false) < MAX_MD5SIZE) {
00128             $md5 = md5_file($this->getFullPath());
00129         }
00130         return $md5;
00131     }
00132 /**
00133  * Returns the Filesize, either in bytes or in human-readable format.
00134  *
00135  * @param boolean $humanReadeble    Data to write to this File.
00136  * @return string|int filesize as int or as a human-readable string
00137  */
00138     function getSize() {
00139         $size = filesize($this->getFullPath());
00140         return $size;
00141     }
00142 /**
00143  * Returns the File extension.
00144  *
00145  * @return string The Fileextension
00146  */
00147     function getExt() {
00148         $ext = '';
00149         $parts = explode('.', $this->getName());
00150 
00151         if (count($parts) > 1) {
00152             $ext = array_pop($parts);
00153         } else {
00154             $ext = '';
00155         }
00156         return $ext;
00157     }
00158 /**
00159  * Returns the filename.
00160  *
00161  * @return string The Filename
00162  */
00163     function getName() {
00164         return $this->name;
00165     }
00166 /**
00167  * Returns the File's owner.
00168  *
00169  * @return int the Fileowner
00170  */
00171     function getOwner() {
00172         $fileowner = fileowner($this->getFullPath());
00173         return $fileowner;
00174      }
00175 /**
00176  * Returns the File group.
00177  *
00178  * @return int the Filegroup
00179  */
00180     function getGroup() {
00181         $filegroup = filegroup($this->getFullPath());
00182         return $filegroup;
00183      }
00184 /**
00185  * Creates the File.
00186  *
00187  * @return boolean Success
00188  */
00189     function create() {
00190         $dir = $this->folder->pwd();
00191 
00192         if (file_exists($dir) && is_dir($dir) && is_writable($dir) && !$this->exists()) {
00193             if (!touch($this->getFullPath())) {
00194                 print ('[File] Could not create '.$this->getName().'!');
00195                 return false;
00196             } else {
00197                 return true;
00198             }
00199         } else {
00200             print ('[File] Could not create '.$this->getName().'!');
00201             return false;
00202         }
00203     }
00204 /**
00205  * Returns true if the File exists.
00206  *
00207  * @return boolean
00208  */
00209     function exists() {
00210         $exists = file_exists($this->getFullPath());
00211         return $exists;
00212     }
00213 /**
00214  * Deletes the File.
00215  *
00216  * @return boolean
00217  */
00218     function delete() {
00219         $unlink = unlink($this->getFullPath());
00220         return $unlink;
00221      }
00222 /**
00223  * Returns true if the File is writable.
00224  *
00225  * @return boolean
00226  */
00227     function writable() {
00228         $writable = is_writable($this->getFullPath());
00229         return $writable;
00230     }
00231 /**
00232  * Returns true if the File is executable.
00233  *
00234  * @return boolean
00235  */
00236     function executable() {
00237         $executable = is_executable($this->getFullPath());
00238         return $executable;
00239     }
00240 /**
00241  * Returns true if the File is readable.
00242  *
00243  * @return boolean
00244  */
00245     function readable() {
00246         $readable = is_readable($this->getFullPath());
00247         return $readable;
00248     }
00249 /**
00250  * Returns last access time.
00251  *
00252  * @return int timestamp
00253  */
00254     function lastAccess() {
00255         $fileatime = fileatime($this->getFullPath());
00256         return $fileatime;
00257      }
00258 /**
00259  * Returns last modified time.
00260  *
00261  * @return int timestamp
00262  */
00263     function lastChange() {
00264         $filemtime = filemtime($this->getFullPath());
00265         return $filemtime;
00266     }
00267 /**
00268  * Returns the current folder.
00269  *
00270  * @return Folder
00271  */
00272     function getFolder() {
00273         return $this->folder;
00274     }
00275 /**
00276  * Returns the "chmod" (permissions) of the File.
00277  *
00278  * @return string
00279  */
00280     function getChmod() {
00281         $substr = substr(sprintf('%o', fileperms($this->getFullPath())), -4);
00282         return $substr;
00283     }
00284 /**
00285  * Returns the full path of the File.
00286  *
00287  * @return string
00288  */
00289     function getFullPath() {
00290         return $this->folder->slashTerm($this->folder->pwd()) . $this->getName();
00291     }
00292 }
00293 ?>