Source for file jFile.class.php

Documentation is available at jFile.class.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage utils
  5. @author Laurent Jouanneau
  6. @contributor Thiriot Christophe
  7. @contributor Loic Mathaud
  8. @contributor Bastien Jaillot
  9. @contributor Cedric (fix bug ticket 56)
  10. @contributor Foxmask (#733)
  11. @copyright   2005-2006 Laurent Jouanneau, 2006 Thiriot Christophe, 2006 Loic Mathaud, 2008 Bastien Jaillot, 2008 Foxmask
  12. @link        http://www.jelix.org
  13. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  14. */
  15.  
  16.  
  17. /**
  18.  * A class helper to read or create files
  19.  * @package    jelix
  20.  * @subpackage utils
  21.  */
  22. class jFile {
  23.     /**
  24.     * Reads the content of a file.
  25.     * @param string $filename the filename we're gonna read
  26.     * @return string the content of the file. false if cannot read the file
  27.     */
  28.     public static function read ($filename){
  29.         return @file_get_contents ($filenamefalse);
  30.     }
  31.  
  32.     /**
  33.     * Write a file to the disk.
  34.     * This function is heavily based on the way smarty process its own files.
  35.     * Is using a temporary file and then rename the file. We guess the file system will be smarter than us, avoiding a writing / reading
  36.     *  while renaming the file.
  37.     * This method comes from CopixFile class of Copix framework
  38.     * @author     Croes GĂ©rald
  39.     * @copyright  2001-2005 CopixTeam
  40.     * @link http://www.copix.org
  41.     */
  42.     public static function write ($file$data){
  43.         $_dirname dirname($file);
  44.  
  45.         //asking to create the directory structure if needed.
  46.         self::createDir ($_dirname);
  47.  
  48.         if(!@is_writable($_dirname)) {
  49.             // cache_dir not writable, see if it exists
  50.             if(!@is_dir($_dirname)) {
  51.                 throw new jException('jelix~errors.file.directory.notexists'array ($_dirname));
  52.             }
  53.             throw new jException('jelix~errors.file.directory.notwritable'array ($file$_dirname));
  54.         }
  55.  
  56.         // write to tmp file, then rename it to avoid
  57.         // file locking race condition
  58.         $_tmp_file tempnam($_dirname'wrt');
  59.  
  60.         if (!($fd @fopen($_tmp_file'wb'))) {
  61.             $_tmp_file $_dirname '/' uniqid('wrt');
  62.             if (!($fd @fopen($_tmp_file'wb'))) {
  63.                 throw new jException('jelix~errors.file.write.error'array ($file$_tmp_file));
  64.             }
  65.         }
  66.  
  67.         fwrite($fd$data);
  68.         fclose($fd);
  69.  
  70.         // Delete the file if it allready exists (this is needed on Win,
  71.         // because it cannot overwrite files with rename()
  72.         if ($GLOBALS['gJConfig']->isWindows && file_exists($file)) {
  73.             unlink($file);
  74.         }
  75.         rename($_tmp_file$file);
  76.         @chmod($file,  0664);
  77.  
  78.         return true;
  79.     }
  80.  
  81.     /**
  82.     * create a directory
  83.     * It creates also all necessary parent directory
  84.     * @param string $dir the path of the directory
  85.     */
  86.     public static function createDir ($dir){
  87.         // recursive feature on mkdir() is broken with PHP 5.0.4 for Windows
  88.         // so should do own recursion
  89.         if (!file_exists($dir)) {
  90.             self::createDir(dirname($dir));
  91.             mkdir($dir0775);
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * Recursive function deleting a directory
  97.      *
  98.      * @param string $path The path of the directory to remove recursively
  99.      * @param boolean $deleteParent If the path must be deleted too
  100.      * @since 1.0b1
  101.      * @author Loic Mathaud
  102.      */
  103.     public static function removeDir($path$deleteParent=true{
  104.         $dir new DirectoryIterator($path);
  105.         foreach ($dir as $dirContent{
  106.             // file deletion
  107.             if ($dirContent->isFile(|| $dirContent->isLink()) {
  108.                 unlink($dirContent->getPathName());
  109.             else {
  110.                 // recursive directory deletion
  111.                 if (!$dirContent->isDot(&& $dirContent->isDir()) {
  112.                     self::removeDir($dirContent->getPathName());
  113.                 }
  114.             }
  115.         }
  116.  
  117.         unset($dir)// see bug #733
  118.         unset($dirContent);
  119.  
  120.         // removes the parent directory
  121.         if ($deleteParent{
  122.             rmdir($path);
  123.         }
  124.     }
  125. }
  126. ?>

Documentation generated on Wed, 07 Sep 2011 13:47:18 +0200 by phpDocumentor 1.4.3