file_helper.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter File Helpers
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Helpers
00023  * @category    Helpers
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/helpers/file_helper.html
00026  */
00027 
00028 // ------------------------------------------------------------------------
00029 
00030 /**
00031  * Read File
00032  *
00033  * Opens the file specfied in the path and returns it as a string.
00034  *
00035  * @access      public
00036  * @param       string  path to file
00037  * @return      string
00038  */     
00039 if ( ! function_exists('read_file'))
00040 {
00041         function read_file($file)
00042         {
00043                 if ( ! file_exists($file))
00044                 {
00045                         return FALSE;
00046                 }
00047         
00048                 if (function_exists('file_get_contents'))
00049                 {
00050                         return file_get_contents($file);                
00051                 }
00052 
00053                 if ( ! $fp = @fopen($file, FOPEN_READ))
00054                 {
00055                         return FALSE;
00056                 }
00057                 
00058                 flock($fp, LOCK_SH);
00059         
00060                 $data = '';
00061                 if (filesize($file) > 0)
00062                 {
00063                         $data =& fread($fp, filesize($file));
00064                 }
00065 
00066                 flock($fp, LOCK_UN);
00067                 fclose($fp);
00068 
00069                 return $data;
00070         }
00071 }
00072         
00073 // ------------------------------------------------------------------------
00074 
00075 /**
00076  * Write File
00077  *
00078  * Writes data to the file specified in the path.
00079  * Creates a new file if non-existent.
00080  *
00081  * @access      public
00082  * @param       string  path to file
00083  * @param       string  file data
00084  * @return      bool
00085  */     
00086 if ( ! function_exists('write_file'))
00087 {
00088         function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
00089         {
00090                 if ( ! $fp = @fopen($path, $mode))
00091                 {
00092                         return FALSE;
00093                 }
00094                 
00095                 flock($fp, LOCK_EX);
00096                 fwrite($fp, $data);
00097                 flock($fp, LOCK_UN);
00098                 fclose($fp);    
00099 
00100                 return TRUE;
00101         }
00102 }
00103         
00104 // ------------------------------------------------------------------------
00105 
00106 /**
00107  * Delete Files
00108  *
00109  * Deletes all files contained in the supplied directory path.
00110  * Files must be writable or owned by the system in order to be deleted.
00111  * If the second parameter is set to TRUE, any directories contained
00112  * within the supplied base directory will be nuked as well.
00113  *
00114  * @access      public
00115  * @param       string  path to file
00116  * @param       bool    whether to delete any directories found in the path
00117  * @return      bool
00118  */     
00119 if ( ! function_exists('delete_files'))
00120 {
00121         function delete_files($path, $del_dir = FALSE, $level = 0)
00122         {       
00123                 // Trim the trailing slash
00124                 $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
00125                 
00126                 if ( ! $current_dir = @opendir($path))
00127                         return;
00128         
00129                 while(FALSE !== ($filename = @readdir($current_dir)))
00130                 {
00131                         if ($filename != "." and $filename != "..")
00132                         {
00133                                 if (is_dir($path.'/'.$filename))
00134                                 {
00135                                         // Ignore empty folders
00136                                         if (substr($filename, 0, 1) != '.')
00137                                         {
00138                                                 delete_files($path.'/'.$filename, $del_dir, $level + 1);
00139                                         }
00140                                 }
00141                                 else
00142                                 {
00143                                         unlink($path.'/'.$filename);
00144                                 }
00145                         }
00146                 }
00147                 @closedir($current_dir);
00148         
00149                 if ($del_dir == TRUE AND $level > 0)
00150                 {
00151                         @rmdir($path);
00152                 }
00153         }
00154 }
00155 
00156 // ------------------------------------------------------------------------
00157 
00158 /**
00159  * Get Filenames
00160  *
00161  * Reads the specified directory and builds an array containing the filenames.  
00162  * Any sub-folders contained within the specified path are read as well.
00163  *
00164  * @access      public
00165  * @param       string  path to source
00166  * @param       bool    whether to include the path as part of the filename
00167  * @param       bool    internal variable to determine recursion status - do not use in calls
00168  * @return      array
00169  */     
00170 if ( ! function_exists('get_filenames'))
00171 {
00172         function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
00173         {
00174                 static $_filedata = array();
00175                                 
00176                 if ($fp = @opendir($source_dir))
00177                 {
00178                         // reset the array and make sure $source_dir has a trailing slash on the initial call
00179                         if ($_recursion === FALSE)
00180                         {
00181                                 $_filedata = array();
00182                                 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
00183                         }
00184                         
00185                         while (FALSE !== ($file = readdir($fp)))
00186                         {
00187                                 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
00188                                 {
00189                                          get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
00190                                 }
00191                                 elseif (strncmp($file, '.', 1) !== 0)
00192                                 {
00193                         
00194                                         $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
00195                                 }
00196                         }
00197                         return $_filedata;
00198                 }
00199                 else
00200                 {
00201                         return FALSE;
00202                 }
00203         }
00204 }
00205 
00206 // --------------------------------------------------------------------
00207 
00208 /**
00209  * Get Directory File Information
00210  *
00211  * Reads the specified directory and builds an array containing the filenames,  
00212  * filesize, dates, and permissions
00213  *
00214  * Any sub-folders contained within the specified path are read as well.
00215  *
00216  * @access      public
00217  * @param       string  path to source
00218  * @param       bool    whether to include the path as part of the filename
00219  * @param       bool    internal variable to determine recursion status - do not use in calls
00220  * @return      array
00221  */     
00222 if ( ! function_exists('get_dir_file_info'))
00223 {
00224         function get_dir_file_info($source_dir, $include_path = FALSE, $_recursion = FALSE)
00225         {
00226                 $_filedata = array();
00227                 $relative_path = $source_dir;
00228                                 
00229                 if ($fp = @opendir($source_dir))
00230                 {
00231                         // reset the array and make sure $source_dir has a trailing slash on the initial call
00232                         if ($_recursion === FALSE)
00233                         {
00234                                 $_filedata = array();
00235                                 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
00236                         }
00237 
00238                         while (FALSE !== ($file = readdir($fp)))
00239                         {
00240                                 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
00241                                 {
00242                                          get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
00243                                 }
00244                                 elseif (strncmp($file, '.', 1) !== 0)
00245                                 {
00246                                         $_filedata[$file] = get_file_info($source_dir.$file);
00247                                         $_filedata[$file]['relative_path'] = $relative_path;
00248                                 }
00249                         }
00250                         return $_filedata;
00251                 }
00252                 else
00253                 {
00254                         return FALSE;
00255                 }
00256         }
00257 }
00258 
00259 // --------------------------------------------------------------------
00260 
00261 /**
00262 * Get File Info
00263 *
00264 * Given a file and path, returns the name, path, size, date modified
00265 * Second parameter allows you to explicitly declare what information you want returned
00266 * Options are: name, server_path, size, date, readable, writable, executable, fileperms
00267 * Returns FALSE if the file cannot be found.
00268 *
00269 * @access       public
00270 * @param        string          path to file
00271 * @param        mixed           array or comma separated string of information returned
00272 * @return       array
00273 */
00274 if ( ! function_exists('get_file_info'))
00275 {
00276         function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
00277         {
00278 
00279                 if ( ! file_exists($file))
00280                 {
00281                         return FALSE;
00282                 }
00283 
00284                 if (is_string($returned_values))
00285                 {
00286                         $returned_values = explode(',', $returned_values);
00287                 }
00288 
00289                 foreach ($returned_values as $key)
00290                 {
00291                         switch ($key)
00292                         {
00293                                 case 'name':
00294                                         $fileinfo['name'] = substr(strrchr($file, '/'), 1);
00295                                         break;
00296                                 case 'server_path':
00297                                         $fileinfo['server_path'] = $file;
00298                                         break;
00299                                 case 'size':
00300                                         $fileinfo['size'] = filesize($file);
00301                                         break;
00302                                 case 'date':
00303                                         $fileinfo['date'] = filectime($file);
00304                                         break;
00305                                 case 'readable':
00306                                         $fileinfo['readable'] = is_readable($file);
00307                                         break;
00308                                 case 'writable':
00309                                         // There are known problems using is_weritable on IIS.  It may not be reliable - consider fileperms()
00310                                         $fileinfo['writable'] = is_writable($file);
00311                                         break;
00312                                 case 'executable':
00313                                         $fileinfo['executable'] = is_executable($file);
00314                                         break;
00315                                 case 'fileperms':
00316                                         $fileinfo['fileperms'] = fileperms($file);
00317                                         break;
00318                         }
00319                 }
00320 
00321                 return $fileinfo;
00322         }
00323 }
00324 
00325 // --------------------------------------------------------------------
00326 
00327 /**
00328  * Get Mime by Extension
00329  *
00330  * Translates a file extension into a mime type based on config/mimes.php. 
00331  * Returns FALSE if it can't determine the type, or open the mime config file
00332  *
00333  * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
00334  * It should NOT be trusted, and should certainly NOT be used for security
00335  *
00336  * @access      public
00337  * @param       string  path to file
00338  * @return      mixed
00339  */     
00340 if ( ! function_exists('get_mime_by_extension'))
00341 {
00342         function get_mime_by_extension($file)
00343         {
00344                 $extension = substr(strrchr($file, '.'), 1);
00345         
00346                 global $mimes;
00347         
00348                 if ( ! is_array($mimes))
00349                 {
00350                         if ( ! require_once(APPPATH.'config/mimes.php'))
00351                         {
00352                                 return FALSE;
00353                         }
00354                 }
00355 
00356                 if (array_key_exists($extension, $mimes))
00357                 {
00358                         if (is_array($mimes[$extension]))
00359                         {
00360                                 // Multiple mime types, just give the first one
00361                                 return current($mimes[$extension]);
00362                         }
00363                         else
00364                         {
00365                                 return $mimes[$extension];
00366                         }
00367                 }
00368                 else
00369                 {
00370                         return FALSE;
00371                 }
00372         }
00373 }
00374 
00375 // --------------------------------------------------------------------
00376 
00377 /**
00378  * Symbolic Permissions
00379  *
00380  * Takes a numeric value representing a file's permissions and returns
00381  * standard symbolic notation representing that value
00382  *
00383  * @access      public
00384  * @param       int
00385  * @return      string
00386  */     
00387 if ( ! function_exists('symbolic_permissions'))
00388 {
00389         function symbolic_permissions($perms)
00390         {       
00391                 if (($perms & 0xC000) == 0xC000)
00392                 {
00393                         $symbolic = 's'; // Socket
00394                 }
00395                 elseif (($perms & 0xA000) == 0xA000)
00396                 {
00397                         $symbolic = 'l'; // Symbolic Link
00398                 }
00399                 elseif (($perms & 0x8000) == 0x8000)
00400                 {
00401                         $symbolic = '-'; // Regular
00402                 }
00403                 elseif (($perms & 0x6000) == 0x6000)
00404                 {
00405                         $symbolic = 'b'; // Block special
00406                 }
00407                 elseif (($perms & 0x4000) == 0x4000)
00408                 {
00409                         $symbolic = 'd'; // Directory
00410                 }
00411                 elseif (($perms & 0x2000) == 0x2000)
00412                 {
00413                         $symbolic = 'c'; // Character special
00414                 }
00415                 elseif (($perms & 0x1000) == 0x1000)
00416                 {
00417                         $symbolic = 'p'; // FIFO pipe
00418                 }
00419                 else
00420                 {
00421                         $symbolic = 'u'; // Unknown
00422                 }
00423 
00424                 // Owner
00425                 $symbolic .= (($perms & 0x0100) ? 'r' : '-');
00426                 $symbolic .= (($perms & 0x0080) ? 'w' : '-');
00427                 $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
00428 
00429                 // Group
00430                 $symbolic .= (($perms & 0x0020) ? 'r' : '-');
00431                 $symbolic .= (($perms & 0x0010) ? 'w' : '-');
00432                 $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
00433 
00434                 // World
00435                 $symbolic .= (($perms & 0x0004) ? 'r' : '-');
00436                 $symbolic .= (($perms & 0x0002) ? 'w' : '-');
00437                 $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
00438 
00439                 return $symbolic;               
00440         }
00441 }
00442 
00443 // --------------------------------------------------------------------
00444 
00445 /**
00446  * Octal Permissions
00447  *
00448  * Takes a numeric value representing a file's permissions and returns
00449  * a three character string representing the file's octal permissions
00450  *
00451  * @access      public
00452  * @param       int
00453  * @return      string
00454  */     
00455 if ( ! function_exists('octal_permissions'))
00456 {
00457         function octal_permissions($perms)
00458         {
00459                 return substr(sprintf('%o', $perms), -3);
00460         }
00461 }
00462 
00463 
00464 /* End of file file_helper.php */
00465 /* Location: ./system/helpers/file_helper.php */