1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * CodeIgniter File Helpers
21 * @package CodeIgniter
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/file_helpers.html
28 // ------------------------------------------------------------------------
33 * Opens the file specfied in the path and returns it as a string.
36 * @param string path to file
39 if ( ! function_exists('read_file'))
41 function read_file($file)
43 if ( ! file_exists($file))
48 if (function_exists('file_get_contents'))
50 return file_get_contents($file);
53 if ( ! $fp = @fopen($file, FOPEN_READ))
61 if (filesize($file) > 0)
63 $data =& fread($fp, filesize($file));
73 // ------------------------------------------------------------------------
78 * Writes data to the file specified in the path.
79 * Creates a new file if non-existent.
82 * @param string path to file
83 * @param string file data
86 if ( ! function_exists('write_file'))
88 function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
90 if ( ! $fp = @fopen($path, $mode))
104 // ------------------------------------------------------------------------
109 * Deletes all files contained in the supplied directory path.
110 * Files must be writable or owned by the system in order to be deleted.
111 * If the second parameter is set to TRUE, any directories contained
112 * within the supplied base directory will be nuked as well.
115 * @param string path to file
116 * @param bool whether to delete any directories found in the path
119 if ( ! function_exists('delete_files'))
121 function delete_files($path, $del_dir = FALSE, $level = 0)
123 // Trim the trailing slash
124 $path = rtrim($path, DIRECTORY_SEPARATOR);
126 if ( ! $current_dir = @opendir($path))
131 while (FALSE !== ($filename = @readdir($current_dir)))
133 if ($filename != "." and $filename != "..")
135 if (is_dir($path.DIRECTORY_SEPARATOR.$filename))
137 // Ignore empty folders
138 if (substr($filename, 0, 1) != '.')
140 delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1);
145 unlink($path.DIRECTORY_SEPARATOR.$filename);
149 @closedir($current_dir);
151 if ($del_dir == TRUE AND $level > 0)
153 return @rmdir($path);
160 // ------------------------------------------------------------------------
165 * Reads the specified directory and builds an array containing the filenames.
166 * Any sub-folders contained within the specified path are read as well.
169 * @param string path to source
170 * @param bool whether to include the path as part of the filename
171 * @param bool internal variable to determine recursion status - do not use in calls
174 if ( ! function_exists('get_filenames'))
176 function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
178 static $_filedata = array();
180 if ($fp = @opendir($source_dir))
182 // reset the array and make sure $source_dir has a trailing slash on the initial call
183 if ($_recursion === FALSE)
185 $_filedata = array();
186 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
189 while (FALSE !== ($file = readdir($fp)))
191 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
193 get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
195 elseif (strncmp($file, '.', 1) !== 0)
197 $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
209 // --------------------------------------------------------------------
212 * Get Directory File Information
214 * Reads the specified directory and builds an array containing the filenames,
215 * filesize, dates, and permissions
217 * Any sub-folders contained within the specified path are read as well.
220 * @param string path to source
221 * @param bool Look only at the top level directory specified?
222 * @param bool internal variable to determine recursion status - do not use in calls
225 if ( ! function_exists('get_dir_file_info'))
227 function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
229 static $_filedata = array();
230 $relative_path = $source_dir;
232 if ($fp = @opendir($source_dir))
234 // reset the array and make sure $source_dir has a trailing slash on the initial call
235 if ($_recursion === FALSE)
237 $_filedata = array();
238 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
241 // foreach (scandir($source_dir, 1) as $file) // In addition to being PHP5+, scandir() is simply not as fast
242 while (FALSE !== ($file = readdir($fp)))
244 if (@is_dir($source_dir.$file) AND strncmp($file, '.', 1) !== 0 AND $top_level_only === FALSE)
246 get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
248 elseif (strncmp($file, '.', 1) !== 0)
250 $_filedata[$file] = get_file_info($source_dir.$file);
251 $_filedata[$file]['relative_path'] = $relative_path;
264 // --------------------------------------------------------------------
269 * Given a file and path, returns the name, path, size, date modified
270 * Second parameter allows you to explicitly declare what information you want returned
271 * Options are: name, server_path, size, date, readable, writable, executable, fileperms
272 * Returns FALSE if the file cannot be found.
275 * @param string path to file
276 * @param mixed array or comma separated string of information returned
279 if ( ! function_exists('get_file_info'))
281 function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
284 if ( ! file_exists($file))
289 if (is_string($returned_values))
291 $returned_values = explode(',', $returned_values);
294 foreach ($returned_values as $key)
299 $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
302 $fileinfo['server_path'] = $file;
305 $fileinfo['size'] = filesize($file);
308 $fileinfo['date'] = filemtime($file);
311 $fileinfo['readable'] = is_readable($file);
314 // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
315 $fileinfo['writable'] = is_writable($file);
318 $fileinfo['executable'] = is_executable($file);
321 $fileinfo['fileperms'] = fileperms($file);
330 // --------------------------------------------------------------------
333 * Get Mime by Extension
335 * Translates a file extension into a mime type based on config/mimes.php.
336 * Returns FALSE if it can't determine the type, or open the mime config file
338 * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
339 * It should NOT be trusted, and should certainly NOT be used for security
342 * @param string path to file
345 if ( ! function_exists('get_mime_by_extension'))
347 function get_mime_by_extension($file)
349 $extension = strtolower(substr(strrchr($file, '.'), 1));
353 if ( ! is_array($mimes))
355 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes'.EXT))
357 include(APPPATH.'config/'.ENVIRONMENT.'/mimes'.EXT);
359 elseif (is_file(APPPATH.'config/mimes'.EXT))
361 include(APPPATH.'config/mimes'.EXT);
364 if ( ! is_array($mimes))
370 if (array_key_exists($extension, $mimes))
372 if (is_array($mimes[$extension]))
374 // Multiple mime types, just give the first one
375 return current($mimes[$extension]);
379 return $mimes[$extension];
389 // --------------------------------------------------------------------
392 * Symbolic Permissions
394 * Takes a numeric value representing a file's permissions and returns
395 * standard symbolic notation representing that value
401 if ( ! function_exists('symbolic_permissions'))
403 function symbolic_permissions($perms)
405 if (($perms & 0xC000) == 0xC000)
407 $symbolic = 's'; // Socket
409 elseif (($perms & 0xA000) == 0xA000)
411 $symbolic = 'l'; // Symbolic Link
413 elseif (($perms & 0x8000) == 0x8000)
415 $symbolic = '-'; // Regular
417 elseif (($perms & 0x6000) == 0x6000)
419 $symbolic = 'b'; // Block special
421 elseif (($perms & 0x4000) == 0x4000)
423 $symbolic = 'd'; // Directory
425 elseif (($perms & 0x2000) == 0x2000)
427 $symbolic = 'c'; // Character special
429 elseif (($perms & 0x1000) == 0x1000)
431 $symbolic = 'p'; // FIFO pipe
435 $symbolic = 'u'; // Unknown
439 $symbolic .= (($perms & 0x0100) ? 'r' : '-');
440 $symbolic .= (($perms & 0x0080) ? 'w' : '-');
441 $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
444 $symbolic .= (($perms & 0x0020) ? 'r' : '-');
445 $symbolic .= (($perms & 0x0010) ? 'w' : '-');
446 $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
449 $symbolic .= (($perms & 0x0004) ? 'r' : '-');
450 $symbolic .= (($perms & 0x0002) ? 'w' : '-');
451 $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
457 // --------------------------------------------------------------------
462 * Takes a numeric value representing a file's permissions and returns
463 * a three character string representing the file's octal permissions
469 if ( ! function_exists('octal_permissions'))
471 function octal_permissions($perms)
473 return substr(sprintf('%o', $perms), -3);
478 /* End of file file_helper.php */
479 /* Location: ./system/helpers/file_helper.php */