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) 2006, 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                                         delete_files($path.'/'.$filename, $del_dir, $level + 1);
00136                                 }
00137                                 else
00138                                 {
00139                                         unlink($path.'/'.$filename);
00140                                 }
00141                         }
00142                 }
00143                 @closedir($current_dir);
00144         
00145                 if ($del_dir == TRUE AND $level > 0)
00146                 {
00147                         @rmdir($path);
00148                 }
00149         }
00150 }
00151 
00152 // ------------------------------------------------------------------------
00153 
00154 /**
00155  * Get Filenames
00156  *
00157  * Reads the specified directory and builds an array containing the filenames.  
00158  * Any sub-folders contained within the specified path are read as well.
00159  *
00160  * @access      public
00161  * @param       string  path to source
00162  * @param       bool    whether to include the path as part of the filename
00163  * @param       bool    internal variable to determine recursion status - do not use in calls
00164  * @return      array
00165  */     
00166 if ( ! function_exists('get_filenames'))
00167 {
00168         function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
00169         {
00170                 static $_filedata = array();
00171                                 
00172                 if ($fp = @opendir($source_dir))
00173                 {
00174                         // reset the array and make sure $source_dir has a trailing slash on the initial call
00175                         if ($_recursion === FALSE)
00176                         {
00177                                 $_filedata = array();
00178                                 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
00179                         }
00180                         
00181                         while (FALSE !== ($file = readdir($fp)))
00182                         {
00183                                 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
00184                                 {
00185                                          get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
00186                                 }
00187                                 elseif (strncmp($file, '.', 1) !== 0)
00188                                 {
00189                         
00190                                         $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
00191                                 }
00192                         }
00193                         return $_filedata;
00194                 }
00195                 else
00196                 {
00197                         return FALSE;
00198                 }
00199         }
00200 }
00201 
00202 // --------------------------------------------------------------------
00203 
00204 /**
00205  * Get Directory File Information
00206  *
00207  * Reads the specified directory and builds an array containing the filenames,  
00208  * filesize, dates, and permissions
00209  *
00210  * Any sub-folders contained within the specified path are read as well.
00211  *
00212  * @access      public
00213  * @param       string  path to source
00214  * @param       bool    whether to include the path as part of the filename
00215  * @param       bool    internal variable to determine recursion status - do not use in calls
00216  * @return      array
00217  */     
00218 if ( ! function_exists('get_dir_file_info'))
00219 {
00220         function get_dir_file_info($source_dir, $include_path = FALSE, $_recursion = FALSE)
00221         {
00222                 $_filedata = array();
00223                 $relative_path = $source_dir;
00224                                 
00225                 if ($fp = @opendir($source_dir))
00226                 {
00227                         // reset the array and make sure $source_dir has a trailing slash on the initial call
00228                         if ($_recursion === FALSE)
00229                         {
00230                                 $_filedata = array();
00231                                 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
00232                         }
00233 
00234                         while (FALSE !== ($file = readdir($fp)))
00235                         {
00236                                 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
00237                                 {
00238                                          get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
00239                                 }
00240                                 elseif (strncmp($file, '.', 1) !== 0)
00241                                 {
00242                                         $_filedata[$file] = get_file_info($source_dir.$file);
00243                                         $_filedata[$file]['relative_path'] = $relative_path;
00244                                 }
00245                         }
00246                         return $_filedata;
00247                 }
00248                 else
00249                 {
00250                         return FALSE;
00251                 }
00252         }
00253 }
00254 
00255 // --------------------------------------------------------------------
00256 
00257 /**
00258 * Get File Info
00259 *
00260 * Given a file and path, returns the name, path, size, date modified
00261 * Second parameter allows you to explicitly declare what information you want returned
00262 * Options are: name, server_path, size, date, readable, writable, executable, fileperms
00263 * Returns FALSE if the file cannot be found.
00264 *
00265 * @access    public
00266 * @param    string    path to file
00267 * @param    mixed    array or comma separated string of information returned
00268 * @return    array
00269 */    
00270 if ( ! function_exists('get_file_info'))
00271 {
00272     function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
00273     {
00274 
00275         if ( ! file_exists($file))
00276         {
00277             return FALSE;
00278         }
00279 
00280         if (is_string($returned_values))
00281         {
00282             $returned_values = explode(',', $returned_values);
00283         }
00284 
00285         foreach ($returned_values as $key)
00286         {
00287             switch ($key)
00288             {
00289                 case 'name':
00290                     $fileinfo['name'] = substr(strrchr($file, '/'), 1);
00291                     break;
00292                 case 'server_path':
00293                     $fileinfo['server_path'] = $file;
00294                     break;
00295                 case 'size':
00296                     $fileinfo['size'] = filesize($file);
00297                     break;
00298                 case 'date':
00299                     $fileinfo['date'] = filectime($file);
00300                     break;
00301                 case 'readable':
00302                     $fileinfo['readable'] = is_readable($file);
00303                     break;
00304                 case 'writable':
00305                     // There are known problems using is_weritable on IIS.  It may not be reliable - consider fileperms()
00306                     $fileinfo['writable'] = is_writable($file);
00307                     break;
00308                 case 'executable':
00309                     $fileinfo['executable'] = is_executable($file);
00310                     break;
00311                 case 'fileperms':
00312                     $fileinfo['fileperms'] = fileperms($file);
00313                     break;
00314             }
00315         }
00316 
00317         return $fileinfo;
00318     }
00319 }
00320 
00321 // --------------------------------------------------------------------
00322 
00323 /**
00324  * Get Mime by Extension
00325  *
00326  * Translates a file extension into a mime type based on config/mimes.php. 
00327  * Returns FALSE if it can't determine the type, or open the mime config file
00328  *
00329  * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
00330  * It should NOT be trusted, and should certainly NOT be used for security
00331  *
00332  * @access      public
00333  * @param       string  path to file
00334  * @return      mixed
00335  */     
00336 if ( ! function_exists('get_mime_by_extension'))
00337 {
00338         function get_mime_by_extension($file)
00339         {
00340                 $extension = substr(strrchr($file, '.'), 1);
00341         
00342                 global $mimes;
00343         
00344                 if ( ! is_array($mimes))
00345                 {
00346                         if ( ! require_once(APPPATH.'config/mimes.php'))
00347                         {
00348                                 return FALSE;
00349                         }
00350                 }
00351 
00352                 if (array_key_exists($extension, $mimes))
00353                 {
00354                         if (is_array($mimes[$extension]))
00355                         {
00356                                 // Multiple mime types, just give the first one
00357                                 return current($mimes[$extension]);
00358                         }
00359                         else
00360                         {
00361                                 return $mimes[$extension];
00362                         }
00363                 }
00364                 else
00365                 {
00366                         return FALSE;
00367                 }
00368         }
00369 }
00370 
00371 // --------------------------------------------------------------------
00372 
00373 /**
00374  * Symbolic Permissions
00375  *
00376  * Takes a numeric value representing a file's permissions and returns
00377  * standard symbolic notation representing that value
00378  *
00379  * @access      public
00380  * @param       int
00381  * @return      string
00382  */     
00383 if ( ! function_exists('symbolic_permissions'))
00384 {
00385         function symbolic_permissions($perms)
00386         {       
00387                 if (($perms & 0xC000) == 0xC000)
00388                 {
00389                         $symbolic = 's'; // Socket
00390                 }
00391                 elseif (($perms & 0xA000) == 0xA000)
00392                 {
00393                         $symbolic = 'l'; // Symbolic Link
00394                 }
00395                 elseif (($perms & 0x8000) == 0x8000)
00396                 {
00397                         $symbolic = '-'; // Regular
00398                 }
00399                 elseif (($perms & 0x6000) == 0x6000)
00400                 {
00401                         $symbolic = 'b'; // Block special
00402                 }
00403                 elseif (($perms & 0x4000) == 0x4000)
00404                 {
00405                         $symbolic = 'd'; // Directory
00406                 }
00407                 elseif (($perms & 0x2000) == 0x2000)
00408                 {
00409                         $symbolic = 'c'; // Character special
00410                 }
00411                 elseif (($perms & 0x1000) == 0x1000)
00412                 {
00413                         $symbolic = 'p'; // FIFO pipe
00414                 }
00415                 else
00416                 {
00417                         $symbolic = 'u'; // Unknown
00418                 }
00419 
00420                 // Owner
00421                 $symbolic .= (($perms & 0x0100) ? 'r' : '-');
00422                 $symbolic .= (($perms & 0x0080) ? 'w' : '-');
00423                 $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
00424 
00425                 // Group
00426                 $symbolic .= (($perms & 0x0020) ? 'r' : '-');
00427                 $symbolic .= (($perms & 0x0010) ? 'w' : '-');
00428                 $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
00429 
00430                 // World
00431                 $symbolic .= (($perms & 0x0004) ? 'r' : '-');
00432                 $symbolic .= (($perms & 0x0002) ? 'w' : '-');
00433                 $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
00434 
00435                 return $symbolic;               
00436         }
00437 }
00438 
00439 // --------------------------------------------------------------------
00440 
00441 /**
00442  * Octal Permissions
00443  *
00444  * Takes a numeric value representing a file's permissions and returns
00445  * a three character string representing the file's octal permissions
00446  *
00447  * @access      public
00448  * @param       int
00449  * @return      string
00450  */     
00451 if ( ! function_exists('octal_permissions'))
00452 {
00453         function octal_permissions($perms)
00454         {
00455                 return substr(sprintf('%o', $perms), -3);
00456         }
00457 }
00458 
00459 
00460 /* End of file file_helper.php */
00461 /* Location: ./system/helpers/file_helper.php */