file.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: file.php 8189 2009-06-04 21:29:26Z DarkAngelBGE $ */
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. (http://www.cakefoundation.org)
00010  *
00011  * Licensed under The MIT License
00012  * Redistributions of files must retain the above copyright notice.
00013  *
00014  * @filesource
00015  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00016  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00017  * @package       cake
00018  * @subpackage    cake.cake.libs
00019  * @since         CakePHP(tm) v 0.2.9
00020  * @version       $Revision: 8189 $
00021  * @modifiedby    $LastChangedBy: DarkAngelBGE $
00022  * @lastmodified  $Date: 2009-06-04 17:29:26 -0400 (Thu, 04 Jun 2009) $
00023  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00024  */
00025 /**
00026  * Included libraries.
00027  *
00028  */
00029 if (!class_exists('Object')) {
00030     uses('object');
00031 }
00032 if (!class_exists('Folder')) {
00033     require LIBS . 'folder.php';
00034 }
00035 /**
00036  * Convenience class for reading, writing and appending to files.
00037  *
00038  * @package       cake
00039  * @subpackage    cake.cake.libs
00040  */
00041 class File extends Object {
00042 /**
00043  * Folder object of the File
00044  *
00045  * @var Folder
00046  * @access public
00047  */
00048     var $Folder = null;
00049 /**
00050  * Filename
00051  *
00052  * @var string
00053  * @access public
00054  */
00055     var $name = null;
00056 /**
00057  * file info
00058  *
00059  * @var string
00060  * @access public
00061  */
00062     var $info = array();
00063 /**
00064  * Holds the file handler resource if the file is opened
00065  *
00066  * @var resource
00067  * @access public
00068  */
00069     var $handle = null;
00070 /**
00071  * enable locking for file reading and writing
00072  *
00073  * @var boolean
00074  * @access public
00075  */
00076     var $lock = null;
00077 /**
00078  * path property
00079  *
00080  * Current file's absolute path
00081  *
00082  * @var mixed null
00083  * @access public
00084  */
00085     var $path = null;
00086 /**
00087  * Constructor
00088  *
00089  * @param string $path Path to file
00090  * @param boolean $create Create file if it does not exist (if true)
00091  * @param integer $mode Mode to apply to the folder holding the file
00092  * @access private
00093  */
00094     function __construct($path, $create = false, $mode = 0755) {
00095         parent::__construct();
00096         $this->Folder =& new Folder(dirname($path), $create, $mode);
00097         if (!is_dir($path)) {
00098             $this->name = basename($path);
00099         }
00100         $this->pwd();
00101 
00102         if (!$this->exists()) {
00103             if ($create === true) {
00104                 if ($this->safe($path) && $this->create() === false) {
00105                     return false;
00106                 }
00107             } else {
00108                 return false;
00109             }
00110         }
00111     }
00112 /**
00113  * Closes the current file if it is opened
00114  *
00115  * @access private
00116  */
00117     function __destruct() {
00118         $this->close();
00119     }
00120 /**
00121  * Creates the File.
00122  *
00123  * @return boolean Success
00124  * @access public
00125  */
00126     function create() {
00127         $dir = $this->Folder->pwd();
00128         if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
00129             $old = umask(0);
00130             if (touch($this->path)) {
00131                 umask($old);
00132                 return true;
00133             }
00134         }
00135         return false;
00136     }
00137 /**
00138  * Opens the current file with a given $mode
00139  *
00140  * @param string $mode A valid 'fopen' mode string (r|w|a ...)
00141  * @param boolean $force If true then the file will be re-opened even if its already opened, otherwise it won't
00142  * @return boolean True on success, false on failure
00143  * @access public
00144  */
00145     function open($mode = 'r', $force = false) {
00146         if (!$force && is_resource($this->handle)) {
00147             return true;
00148         }
00149         clearstatcache();
00150         if ($this->exists() === false) {
00151             if ($this->create() === false) {
00152                 return false;
00153             }
00154         }
00155 
00156         $this->handle = fopen($this->path, $mode);
00157         if (is_resource($this->handle)) {
00158             return true;
00159         }
00160         return false;
00161     }
00162 /**
00163  * Return the contents of this File as a string.
00164  *
00165  * @param string $bytes where to start
00166  * @param string $mode
00167  * @param boolean $force If true then the file will be re-opened even if its already opened, otherwise it won't
00168  * @return mixed string on success, false on failure
00169  * @access public
00170  */
00171     function read($bytes = false, $mode = 'rb', $force = false) {
00172         if ($bytes === false && $this->lock === null) {
00173             return file_get_contents($this->path);
00174         }
00175         if ($this->open($mode, $force) === false) {
00176             return false;
00177         }
00178         if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
00179             return false;
00180         }
00181         if (is_int($bytes)) {
00182             return fread($this->handle, $bytes);
00183         }
00184 
00185         $data = '';
00186         while (!feof($this->handle)) {
00187             $data .= fgets($this->handle, 4096);
00188         }
00189         $data = trim($data);
00190 
00191         if ($this->lock !== null) {
00192             flock($this->handle, LOCK_UN);
00193         }
00194         if ($bytes === false) {
00195             $this->close();
00196         }
00197         return $data;
00198     }
00199 /**
00200  * Sets or gets the offset for the currently opened file.
00201  *
00202  * @param mixed $offset The $offset in bytes to seek. If set to false then the current offset is returned.
00203  * @param integer $seek PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to
00204  * @return mixed True on success, false on failure (set mode), false on failure or integer offset on success (get mode)
00205  * @access public
00206  */
00207     function offset($offset = false, $seek = SEEK_SET) {
00208         if ($offset === false) {
00209             if (is_resource($this->handle)) {
00210                 return ftell($this->handle);
00211             }
00212         } elseif ($this->open() === true) {
00213             return fseek($this->handle, $offset, $seek) === 0;
00214         }
00215         return false;
00216     }
00217 /**
00218  * Prepares a ascii string for writing
00219  * fixes line endings
00220  *
00221  * @param string $data Data to prepare for writing.
00222  * @return string
00223  * @access public
00224  */
00225     function prepare($data, $forceWindows = false) {
00226         $lineBreak = "\n";
00227         if (DIRECTORY_SEPARATOR == '\\' || $forceWindows === true) {
00228             $lineBreak = "\r\n";
00229         }
00230         return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak));
00231     }
00232 
00233 /**
00234  * Write given data to this File.
00235  *
00236  * @param string $data  Data to write to this File.
00237  * @param string $mode  Mode of writing. {@link http://php.net/fwrite See fwrite()}.
00238  * @param string $force force the file to open
00239  * @return boolean Success
00240  * @access public
00241  */
00242     function write($data, $mode = 'w', $force = false) {
00243         $success = false;
00244         if ($this->open($mode, $force) === true) {
00245             if ($this->lock !== null) {
00246                 if (flock($this->handle, LOCK_EX) === false) {
00247                     return false;
00248                 }
00249             }
00250 
00251             if (fwrite($this->handle, $data) !== false) {
00252                 $success = true;
00253             }
00254             if ($this->lock !== null) {
00255                 flock($this->handle, LOCK_UN);
00256             }
00257         }
00258         return $success;
00259     }
00260 /**
00261  * Append given data string to this File.
00262  *
00263  * @param string $data Data to write
00264  * @param string $force force the file to open
00265  * @return boolean Success
00266  * @access public
00267  */
00268     function append($data, $force = false) {
00269         return $this->write($data, 'a', $force);
00270     }
00271 /**
00272  * Closes the current file if it is opened.
00273  *
00274  * @return boolean True if closing was successful or file was already closed, otherwise false
00275  * @access public
00276  */
00277     function close() {
00278         if (!is_resource($this->handle)) {
00279             return true;
00280         }
00281         return fclose($this->handle);
00282     }
00283 /**
00284  * Deletes the File.
00285  *
00286  * @return boolean Success
00287  * @access public
00288  */
00289     function delete() {
00290         clearstatcache();
00291         if ($this->exists()) {
00292             return unlink($this->path);
00293         }
00294         return false;
00295     }
00296 /**
00297  * Returns the File extension.
00298  *
00299  * @return string The File extension
00300  * @access public
00301  */
00302     function info() {
00303         if ($this->info == null) {
00304             $this->info = pathinfo($this->path);
00305         }
00306         if (!isset($this->info['filename'])) {
00307             $this->info['filename'] = $this->name();
00308         }
00309         return $this->info;
00310     }
00311 /**
00312  * Returns the File extension.
00313  *
00314  * @return string The File extension
00315  * @access public
00316  */
00317     function ext() {
00318         if ($this->info == null) {
00319             $this->info();
00320         }
00321         if (isset($this->info['extension'])) {
00322             return $this->info['extension'];
00323         }
00324         return false;
00325     }
00326 /**
00327  * Returns the File name without extension.
00328  *
00329  * @return string The File name without extension.
00330  * @access public
00331  */
00332     function name() {
00333         if ($this->info == null) {
00334             $this->info();
00335         }
00336         if (isset($this->info['extension'])) {
00337             return basename($this->name, '.'.$this->info['extension']);
00338         } elseif ($this->name) {
00339             return $this->name;
00340         }
00341         return false;
00342     }
00343 /**
00344  * makes filename safe for saving
00345  *
00346  * @param string $name the name of the file to make safe if different from $this->name
00347  * @return string $ext the extension of the file
00348  * @access public
00349  */
00350     function safe($name = null, $ext = null) {
00351         if (!$name) {
00352             $name = $this->name;
00353         }
00354         if (!$ext) {
00355             $ext = $this->ext();
00356         }
00357         return preg_replace( "/(?:[^\w\.-]+)/", "_", basename($name, $ext));
00358     }
00359 /**
00360  * Get md5 Checksum of file with previous check of Filesize
00361  *
00362  * @param mixed $maxsize in MB or true to force
00363  * @return string md5 Checksum {@link http://php.net/md5_file See md5_file()}
00364  * @access public
00365  */
00366     function md5($maxsize = 5) {
00367         if ($maxsize === true) {
00368             return md5_file($this->path);
00369         }
00370 
00371         $size = $this->size();
00372         if ($size && $size < ($maxsize * 1024) * 1024) {
00373             return md5_file($this->path);
00374         }
00375 
00376         return false;
00377     }
00378 /**
00379  * Returns the full path of the File.
00380  *
00381  * @return string Full path to file
00382  * @access public
00383  */
00384     function pwd() {
00385         if (is_null($this->path)) {
00386             $this->path = $this->Folder->slashTerm($this->Folder->pwd()) . $this->name;
00387         }
00388         return $this->path;
00389     }
00390 /**
00391  * Returns true if the File exists.
00392  *
00393  * @return boolean true if it exists, false otherwise
00394  * @access public
00395  */
00396     function exists() {
00397         return (file_exists($this->path) && is_file($this->path));
00398     }
00399 /**
00400  * Returns the "chmod" (permissions) of the File.
00401  *
00402  * @return string Permissions for the file
00403  * @access public
00404  */
00405     function perms() {
00406         if ($this->exists()) {
00407             return substr(sprintf('%o', fileperms($this->path)), -4);
00408         }
00409         return false;
00410     }
00411 /**
00412  * Returns the Filesize
00413  *
00414  * @return integer size of the file in bytes, or false in case of an error
00415  * @access public
00416  */
00417     function size() {
00418         if ($this->exists()) {
00419             return filesize($this->path);
00420         }
00421         return false;
00422     }
00423 /**
00424  * Returns true if the File is writable.
00425  *
00426  * @return boolean true if its writable, false otherwise
00427  * @access public
00428  */
00429     function writable() {
00430         return is_writable($this->path);
00431     }
00432 /**
00433  * Returns true if the File is executable.
00434  *
00435  * @return boolean true if its executable, false otherwise
00436  * @access public
00437  */
00438     function executable() {
00439         return is_executable($this->path);
00440     }
00441 /**
00442  * Returns true if the File is readable.
00443  *
00444  * @return boolean true if file is readable, false otherwise
00445  * @access public
00446  */
00447     function readable() {
00448         return is_readable($this->path);
00449     }
00450 /**
00451  * Returns the File's owner.
00452  *
00453  * @return integer the Fileowner
00454  * @access public
00455  */
00456     function owner() {
00457         if ($this->exists()) {
00458             return fileowner($this->path);
00459         }
00460         return false;
00461     }
00462 /**
00463  * Returns the File group.
00464  *
00465  * @return integer the Filegroup
00466  * @access public
00467  */
00468     function group() {
00469         if ($this->exists()) {
00470             return filegroup($this->path);
00471         }
00472         return false;
00473     }
00474 /**
00475  * Returns last access time.
00476  *
00477  * @return integer timestamp Timestamp of last access time
00478  * @access public
00479  */
00480     function lastAccess() {
00481         if ($this->exists()) {
00482             return fileatime($this->path);
00483         }
00484         return false;
00485     }
00486 /**
00487  * Returns last modified time.
00488  *
00489  * @return integer timestamp Timestamp of last modification
00490  * @access public
00491  */
00492     function lastChange() {
00493         if ($this->exists()) {
00494             return filemtime($this->path);
00495         }
00496         return false;
00497     }
00498 /**
00499  * Returns the current folder.
00500  *
00501  * @return Folder Current folder
00502  * @access public
00503  */
00504     function &Folder() {
00505         return $this->Folder;
00506     }
00507 }
00508 ?>

Generated on Sun Nov 22 00:30:52 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7