Source for file file_functions.php

Documentation is available at file_functions.php

  1. <?php
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 4.3.2 or newer
  6.  *
  7.  * @package        CodeIgniter
  8.  * @subpackage     Helpers
  9.  * @author        ExpressionEngine Dev Team
  10.  * @copyright    Copyright (c) 2006, EllisLab, Inc.
  11.  * @license        http://codeigniter.com/user_guide/license.html
  12.  * @link        http://codeigniter.com
  13.  * @since        Version 1.0
  14.  * @filesource
  15.  */
  16.  
  17. // ------------------------------------------------------------------------
  18.  
  19. /**
  20.  * CodeIgniter File Helpers
  21.  *
  22.  * @author        ExpressionEngine Dev Team
  23.  * @link        http://codeigniter.com/user_guide/helpers/file_helpers.html
  24.  */
  25.  
  26. // ------------------------------------------------------------------------
  27.  
  28. if (function_exists('read_file')) {
  29.     /**
  30.      * Read File
  31.      *
  32.      * Opens the file specfied in the path and returns it as a string.
  33.      *
  34.      * @access    public
  35.      * @param    string    path to file
  36.      * @return    string 
  37.      */
  38.     function read_file($file)
  39.     {
  40.         if file_exists($file))
  41.         {
  42.             return FALSE;
  43.         }
  44.     
  45.         if (function_exists('file_get_contents'))
  46.         {
  47.             return file_get_contents($file);        
  48.         }
  49.  
  50.         if $fp @fopen($file'rb'))
  51.         {
  52.             return FALSE;
  53.         }
  54.         
  55.         flock($fpLOCK_SH);
  56.     
  57.         $data '';
  58.         if (filesize($file0)
  59.         {
  60.             $data =fread($fpfilesize($file));
  61.         }
  62.  
  63.         flock($fpLOCK_UN);
  64.         fclose($fp);
  65.  
  66.         return $data;
  67.     }
  68. }
  69.     
  70. // ------------------------------------------------------------------------
  71.  
  72.  
  73. if (function_exists('write_file')) {
  74.     /**
  75.      * Write File
  76.      *
  77.      * Writes data to the file specified in the path.
  78.      * Creates a new file if non-existent.
  79.      *
  80.      * @access    public
  81.      * @param    string    path to file
  82.      * @param    string    file data
  83.      * @return    bool 
  84.      */
  85.     function write_file($path$data$mode 'wb')
  86.     {
  87.         if $fp @fopen($path$mode))
  88.         {
  89.             return FALSE;
  90.         }
  91.         
  92.         flock($fpLOCK_EX);
  93.         fwrite($fp$data);
  94.         flock($fpLOCK_UN);
  95.         fclose($fp);    
  96.  
  97.         return TRUE;
  98.     }
  99. }
  100.     
  101. // ------------------------------------------------------------------------
  102.  
  103. if (function_exists('delete_files')) {
  104.     /**
  105.      * Delete Files
  106.      *
  107.      * Deletes all files contained in the supplied directory path.
  108.      * Files must be writable or owned by the system in order to be deleted.
  109.      * If the second parameter is set to TRUE, any directories contained
  110.      * within the supplied base directory will be nuked as well.
  111.      *
  112.      * @access    public
  113.      * @param    string    path to file
  114.      * @param    bool    whether to delete any directories found in the path
  115.      * @return    bool 
  116.      */
  117.     function delete_files($path$del_dir FALSE$level 0)
  118.     {    
  119.         // Trim the trailing slash
  120.         $path preg_replace("|^(.+?)/*$|""\\1"$path);
  121.         
  122.         if $current_dir @opendir($path))
  123.             return;
  124.     
  125.         while(FALSE !== ($filename @readdir($current_dir)))
  126.         {
  127.             if ($filename != "." and $filename != "..")
  128.             {
  129.                 if (is_dir($path.'/'.$filename))
  130.                 {
  131.                     delete_files($path.'/'.$filename$del_dir$level 1);
  132.                 }
  133.                 else
  134.                 {
  135.                     unlink($path.'/'.$filename);
  136.                 }
  137.             }
  138.         }
  139.         @closedir($current_dir);
  140.     
  141.         if ($del_dir == TRUE AND $level 0)
  142.         {
  143.             @rmdir($path);
  144.         }
  145.     }
  146. }
  147.  
  148. // ------------------------------------------------------------------------
  149.  
  150. if (function_exists('get_filenames')) {
  151.     /**
  152.      * Get Filenames
  153.      *
  154.      * Reads the specified directory and builds an array containing the filenames.
  155.      * Any sub-folders contained within the specified path are read as well.
  156.      *
  157.      * @access    public
  158.      * @param    string    path to source
  159.      * @param    bool    whether to include the path as part of the filename
  160.      * @return    array 
  161.      */
  162.     function get_filenames($source_dir$include_path FALSE)
  163.     {
  164.         $_filedata array();
  165.     
  166.         if ($fp @opendir($source_dir))
  167.         {
  168.             while (FALSE !== ($file readdir($fp)))
  169.             {
  170.                 if (@is_dir($source_dir.$file&& substr($file01!= '.')
  171.                 {
  172.                      get_filenames($source_dir.$file."/"$include_path);
  173.                 }
  174.                 elseif (substr($file01!= ".")
  175.                 {
  176.             
  177.                     $_filedata[($include_path == TRUE$source_dir.$file $file;
  178.                 }
  179.             }
  180.             return $_filedata;
  181.         }
  182.     }
  183. }
  184.  
  185. // --------------------------------------------------------------------
  186.  
  187. ?>

Documentation generated on Tue, 22 Nov 2011 13:28:55 -0200 by phpDocumentor 1.4.3