blob: 5b508538170163288415cd365814a6376acc3f83 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter File Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Jonesc37f0e12009-04-21 13:33:40 +000037 * @link http://codeigniter.com/user_guide/helpers/file_helpers.html
Derek Allard2067d1a2008-11-13 22:59:24 +000038 */
39
40// ------------------------------------------------------------------------
41
42/**
43 * Read File
44 *
45 * Opens the file specfied in the path and returns it as a string.
46 *
47 * @access public
48 * @param string path to file
49 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020050 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051if ( ! function_exists('read_file'))
52{
53 function read_file($file)
54 {
55 if ( ! file_exists($file))
56 {
57 return FALSE;
58 }
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 if (function_exists('file_get_contents'))
61 {
Barry Mienydd671972010-10-04 16:33:58 +020062 return file_get_contents($file);
Derek Allard2067d1a2008-11-13 22:59:24 +000063 }
64
65 if ( ! $fp = @fopen($file, FOPEN_READ))
66 {
67 return FALSE;
68 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +020071
Derek Allard2067d1a2008-11-13 22:59:24 +000072 $data = '';
73 if (filesize($file) > 0)
74 {
75 $data =& fread($fp, filesize($file));
76 }
77
78 flock($fp, LOCK_UN);
79 fclose($fp);
80
81 return $data;
82 }
83}
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Allard2067d1a2008-11-13 22:59:24 +000085// ------------------------------------------------------------------------
86
87/**
88 * Write File
89 *
90 * Writes data to the file specified in the path.
91 * Creates a new file if non-existent.
92 *
93 * @access public
94 * @param string path to file
95 * @param string file data
96 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020097 */
Derek Allard2067d1a2008-11-13 22:59:24 +000098if ( ! function_exists('write_file'))
99{
100 function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
101 {
102 if ( ! $fp = @fopen($path, $mode))
103 {
104 return FALSE;
105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 flock($fp, LOCK_EX);
108 fwrite($fp, $data);
109 flock($fp, LOCK_UN);
Barry Mienydd671972010-10-04 16:33:58 +0200110 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000111
112 return TRUE;
113 }
114}
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116// ------------------------------------------------------------------------
117
118/**
119 * Delete Files
120 *
121 * Deletes all files contained in the supplied directory path.
122 * Files must be writable or owned by the system in order to be deleted.
123 * If the second parameter is set to TRUE, any directories contained
124 * within the supplied base directory will be nuked as well.
125 *
126 * @access public
127 * @param string path to file
128 * @param bool whether to delete any directories found in the path
129 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200130 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000131if ( ! function_exists('delete_files'))
132{
133 function delete_files($path, $del_dir = FALSE, $level = 0)
Barry Mienydd671972010-10-04 16:33:58 +0200134 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // Trim the trailing slash
Derek Jonesc37f0e12009-04-21 13:33:40 +0000136 $path = rtrim($path, DIRECTORY_SEPARATOR);
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 if ( ! $current_dir = @opendir($path))
Greg Aker3e9519e2010-01-15 00:09:34 +0000139 {
Barry Mienydd671972010-10-04 16:33:58 +0200140 return FALSE;
Greg Aker3e9519e2010-01-15 00:09:34 +0000141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500143 while (FALSE !== ($filename = @readdir($current_dir)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
145 if ($filename != "." and $filename != "..")
146 {
Derek Jonesc37f0e12009-04-21 13:33:40 +0000147 if (is_dir($path.DIRECTORY_SEPARATOR.$filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
149 // Ignore empty folders
150 if (substr($filename, 0, 1) != '.')
151 {
Derek Jonesc37f0e12009-04-21 13:33:40 +0000152 delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1);
Barry Mienydd671972010-10-04 16:33:58 +0200153 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
155 else
156 {
Derek Jonesc37f0e12009-04-21 13:33:40 +0000157 unlink($path.DIRECTORY_SEPARATOR.$filename);
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 }
159 }
160 }
161 @closedir($current_dir);
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 if ($del_dir == TRUE AND $level > 0)
164 {
Greg Aker3e9519e2010-01-15 00:09:34 +0000165 return @rmdir($path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Greg Aker3e9519e2010-01-15 00:09:34 +0000168 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170}
171
172// ------------------------------------------------------------------------
173
174/**
175 * Get Filenames
176 *
Barry Mienydd671972010-10-04 16:33:58 +0200177 * Reads the specified directory and builds an array containing the filenames.
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 * Any sub-folders contained within the specified path are read as well.
179 *
180 * @access public
181 * @param string path to source
182 * @param bool whether to include the path as part of the filename
183 * @param bool internal variable to determine recursion status - do not use in calls
184 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200185 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000186if ( ! function_exists('get_filenames'))
187{
188 function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
189 {
190 static $_filedata = array();
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 if ($fp = @opendir($source_dir))
193 {
194 // reset the array and make sure $source_dir has a trailing slash on the initial call
195 if ($_recursion === FALSE)
196 {
197 $_filedata = array();
198 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 while (FALSE !== ($file = readdir($fp)))
202 {
203 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
204 {
Barry Mienydd671972010-10-04 16:33:58 +0200205 get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
207 elseif (strncmp($file, '.', 1) !== 0)
208 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
210 }
211 }
212 return $_filedata;
213 }
214 else
215 {
216 return FALSE;
217 }
218 }
219}
220
221// --------------------------------------------------------------------
222
223/**
224 * Get Directory File Information
225 *
Barry Mienydd671972010-10-04 16:33:58 +0200226 * Reads the specified directory and builds an array containing the filenames,
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 * filesize, dates, and permissions
228 *
229 * Any sub-folders contained within the specified path are read as well.
230 *
231 * @access public
232 * @param string path to source
Derek Jones626d39f2010-03-02 23:14:56 -0600233 * @param bool Look only at the top level directory specified?
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * @param bool internal variable to determine recursion status - do not use in calls
235 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200236 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000237if ( ! function_exists('get_dir_file_info'))
238{
Derek Jones788b00f2009-11-27 18:00:20 +0000239 function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Derek Jones63dc27f2009-02-10 21:59:20 +0000241 static $_filedata = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 $relative_path = $source_dir;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 if ($fp = @opendir($source_dir))
245 {
246 // reset the array and make sure $source_dir has a trailing slash on the initial call
247 if ($_recursion === FALSE)
248 {
249 $_filedata = array();
250 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
251 }
252
Derek Jones788b00f2009-11-27 18:00:20 +0000253 // foreach (scandir($source_dir, 1) as $file) // In addition to being PHP5+, scandir() is simply not as fast
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 while (FALSE !== ($file = readdir($fp)))
255 {
Derek Jones788b00f2009-11-27 18:00:20 +0000256 if (@is_dir($source_dir.$file) AND strncmp($file, '.', 1) !== 0 AND $top_level_only === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Barry Mienydd671972010-10-04 16:33:58 +0200258 get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
259 }
260 elseif (strncmp($file, '.', 1) !== 0)
261 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 $_filedata[$file] = get_file_info($source_dir.$file);
263 $_filedata[$file]['relative_path'] = $relative_path;
264 }
265 }
Derek Jones788b00f2009-11-27 18:00:20 +0000266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 return $_filedata;
268 }
269 else
270 {
271 return FALSE;
272 }
273 }
274}
275
276// --------------------------------------------------------------------
277
278/**
279* Get File Info
280*
281* Given a file and path, returns the name, path, size, date modified
282* Second parameter allows you to explicitly declare what information you want returned
283* Options are: name, server_path, size, date, readable, writable, executable, fileperms
284* Returns FALSE if the file cannot be found.
285*
286* @access public
Derek Jonesc37f0e12009-04-21 13:33:40 +0000287* @param string path to file
288* @param mixed array or comma separated string of information returned
Derek Allard2067d1a2008-11-13 22:59:24 +0000289* @return array
290*/
291if ( ! function_exists('get_file_info'))
292{
293 function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
294 {
295
296 if ( ! file_exists($file))
297 {
298 return FALSE;
299 }
300
301 if (is_string($returned_values))
302 {
303 $returned_values = explode(',', $returned_values);
304 }
305
306 foreach ($returned_values as $key)
307 {
308 switch ($key)
309 {
310 case 'name':
Derek Jonesc37f0e12009-04-21 13:33:40 +0000311 $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 break;
313 case 'server_path':
314 $fileinfo['server_path'] = $file;
315 break;
316 case 'size':
317 $fileinfo['size'] = filesize($file);
318 break;
319 case 'date':
Robin Sowell73c19fc2010-04-09 11:18:26 -0400320 $fileinfo['date'] = filemtime($file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 break;
322 case 'readable':
323 $fileinfo['readable'] = is_readable($file);
324 break;
325 case 'writable':
Derek Jones37f4b9c2011-07-01 17:56:50 -0500326 // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 $fileinfo['writable'] = is_writable($file);
328 break;
329 case 'executable':
330 $fileinfo['executable'] = is_executable($file);
331 break;
332 case 'fileperms':
333 $fileinfo['fileperms'] = fileperms($file);
334 break;
335 }
336 }
337
338 return $fileinfo;
339 }
340}
341
342// --------------------------------------------------------------------
343
344/**
345 * Get Mime by Extension
346 *
Barry Mienydd671972010-10-04 16:33:58 +0200347 * Translates a file extension into a mime type based on config/mimes.php.
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 * Returns FALSE if it can't determine the type, or open the mime config file
349 *
350 * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
351 * It should NOT be trusted, and should certainly NOT be used for security
352 *
353 * @access public
354 * @param string path to file
355 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000357if ( ! function_exists('get_mime_by_extension'))
358{
359 function get_mime_by_extension($file)
360 {
Derek Allard95023112010-01-17 07:51:21 +0000361 $extension = strtolower(substr(strrchr($file, '.'), 1));
Derek Jonesc37f0e12009-04-21 13:33:40 +0000362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 global $mimes;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 if ( ! is_array($mimes))
366 {
Greg Aker3a746652011-04-19 10:59:47 -0500367 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500368 {
Greg Aker3a746652011-04-19 10:59:47 -0500369 include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500370 }
Greg Aker3a746652011-04-19 10:59:47 -0500371 elseif (is_file(APPPATH.'config/mimes.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500372 {
Greg Aker3a746652011-04-19 10:59:47 -0500373 include(APPPATH.'config/mimes.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500374 }
Eric Barnes92808342011-03-18 09:02:37 -0400375
bubbafoley0ea04142011-03-17 14:55:41 -0500376 if ( ! is_array($mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 return FALSE;
379 }
380 }
381
382 if (array_key_exists($extension, $mimes))
383 {
384 if (is_array($mimes[$extension]))
385 {
386 // Multiple mime types, just give the first one
387 return current($mimes[$extension]);
388 }
389 else
390 {
391 return $mimes[$extension];
392 }
393 }
394 else
395 {
396 return FALSE;
397 }
398 }
399}
400
401// --------------------------------------------------------------------
402
403/**
404 * Symbolic Permissions
405 *
406 * Takes a numeric value representing a file's permissions and returns
407 * standard symbolic notation representing that value
408 *
409 * @access public
410 * @param int
411 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200412 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000413if ( ! function_exists('symbolic_permissions'))
414{
415 function symbolic_permissions($perms)
Barry Mienydd671972010-10-04 16:33:58 +0200416 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 if (($perms & 0xC000) == 0xC000)
418 {
419 $symbolic = 's'; // Socket
420 }
421 elseif (($perms & 0xA000) == 0xA000)
422 {
423 $symbolic = 'l'; // Symbolic Link
424 }
425 elseif (($perms & 0x8000) == 0x8000)
426 {
427 $symbolic = '-'; // Regular
428 }
429 elseif (($perms & 0x6000) == 0x6000)
430 {
431 $symbolic = 'b'; // Block special
432 }
433 elseif (($perms & 0x4000) == 0x4000)
434 {
435 $symbolic = 'd'; // Directory
436 }
437 elseif (($perms & 0x2000) == 0x2000)
438 {
439 $symbolic = 'c'; // Character special
440 }
441 elseif (($perms & 0x1000) == 0x1000)
442 {
443 $symbolic = 'p'; // FIFO pipe
444 }
445 else
446 {
447 $symbolic = 'u'; // Unknown
448 }
449
450 // Owner
451 $symbolic .= (($perms & 0x0100) ? 'r' : '-');
452 $symbolic .= (($perms & 0x0080) ? 'w' : '-');
453 $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
454
455 // Group
456 $symbolic .= (($perms & 0x0020) ? 'r' : '-');
457 $symbolic .= (($perms & 0x0010) ? 'w' : '-');
458 $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
459
460 // World
461 $symbolic .= (($perms & 0x0004) ? 'r' : '-');
462 $symbolic .= (($perms & 0x0002) ? 'w' : '-');
463 $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
464
Barry Mienydd671972010-10-04 16:33:58 +0200465 return $symbolic;
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
467}
468
469// --------------------------------------------------------------------
470
471/**
472 * Octal Permissions
473 *
474 * Takes a numeric value representing a file's permissions and returns
475 * a three character string representing the file's octal permissions
476 *
477 * @access public
478 * @param int
479 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200480 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000481if ( ! function_exists('octal_permissions'))
482{
483 function octal_permissions($perms)
484 {
485 return substr(sprintf('%o', $perms), -3);
486 }
487}
488
489
490/* End of file file_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000491/* Location: ./system/helpers/file_helper.php */