blob: 6378784f47c79d2fbc27611a0752c17dd48462cb [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Rick Ellis86d721c2008-09-12 23:33:40 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter File Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jonesfd93d222008-05-06 15:18:50 +000025 * @link http://codeigniter.com/user_guide/helpers/file_helper.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000026 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Read File
32 *
33 * Opens the file specfied in the path and returns it as a string.
34 *
35 * @access public
36 * @param string path to file
37 * @return string
38 */
Derek Jones0b59f272008-05-13 04:22:33 +000039if ( ! function_exists('read_file'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000040{
Derek Jones269b9422008-01-28 21:00:20 +000041 function read_file($file)
Derek Allardd2df9bc2007-04-15 17:41:17 +000042 {
Derek Jones0b59f272008-05-13 04:22:33 +000043 if ( ! file_exists($file))
Derek Jones269b9422008-01-28 21:00:20 +000044 {
45 return FALSE;
46 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000047
Derek Jones269b9422008-01-28 21:00:20 +000048 if (function_exists('file_get_contents'))
49 {
50 return file_get_contents($file);
51 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000052
Derek Jones0b59f272008-05-13 04:22:33 +000053 if ( ! $fp = @fopen($file, FOPEN_READ))
Derek Jones269b9422008-01-28 21:00:20 +000054 {
55 return FALSE;
56 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000057
Derek Jones269b9422008-01-28 21:00:20 +000058 flock($fp, LOCK_SH);
Derek Allardd2df9bc2007-04-15 17:41:17 +000059
Derek Jones269b9422008-01-28 21:00:20 +000060 $data = '';
61 if (filesize($file) > 0)
62 {
63 $data =& fread($fp, filesize($file));
64 }
65
66 flock($fp, LOCK_UN);
67 fclose($fp);
68
69 return $data;
Derek Allardd2df9bc2007-04-15 17:41:17 +000070 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000071}
72
73// ------------------------------------------------------------------------
74
75/**
76 * Write File
77 *
78 * Writes data to the file specified in the path.
79 * Creates a new file if non-existent.
80 *
81 * @access public
82 * @param string path to file
83 * @param string file data
84 * @return bool
85 */
Derek Jones0b59f272008-05-13 04:22:33 +000086if ( ! function_exists('write_file'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000087{
Derek Jones3be20e22008-05-05 20:07:09 +000088 function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
Derek Allardd2df9bc2007-04-15 17:41:17 +000089 {
Derek Jones9e112202008-05-28 23:46:28 +000090 if ( ! $fp = @fopen($path, $mode))
Derek Jones269b9422008-01-28 21:00:20 +000091 {
92 return FALSE;
93 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000094
Derek Jones269b9422008-01-28 21:00:20 +000095 flock($fp, LOCK_EX);
96 fwrite($fp, $data);
97 flock($fp, LOCK_UN);
98 fclose($fp);
Derek Allardd2df9bc2007-04-15 17:41:17 +000099
Derek Jones269b9422008-01-28 21:00:20 +0000100 return TRUE;
101 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000102}
103
104// ------------------------------------------------------------------------
105
106/**
107 * Delete Files
108 *
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.
113 *
114 * @access public
115 * @param string path to file
116 * @param bool whether to delete any directories found in the path
117 * @return bool
118 */
Derek Jones0b59f272008-05-13 04:22:33 +0000119if ( ! function_exists('delete_files'))
Derek Jones269b9422008-01-28 21:00:20 +0000120{
121 function delete_files($path, $del_dir = FALSE, $level = 0)
122 {
123 // Trim the trailing slash
124 $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
Derek Jones1c8dd3a2008-01-29 20:20:00 +0000125
Derek Jones0b59f272008-05-13 04:22:33 +0000126 if ( ! $current_dir = @opendir($path))
Derek Jones269b9422008-01-28 21:00:20 +0000127 return;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000128
Derek Jones269b9422008-01-28 21:00:20 +0000129 while(FALSE !== ($filename = @readdir($current_dir)))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000130 {
Derek Jones269b9422008-01-28 21:00:20 +0000131 if ($filename != "." and $filename != "..")
Derek Allardd2df9bc2007-04-15 17:41:17 +0000132 {
Derek Jones269b9422008-01-28 21:00:20 +0000133 if (is_dir($path.'/'.$filename))
134 {
Rick Ellisb4a6cf32008-10-06 18:39:36 +0000135 // Ignore empty folders
136 if (substr($filename, 0, 1) != '.')
137 {
138 delete_files($path.'/'.$filename, $del_dir, $level + 1);
139 }
Derek Jones269b9422008-01-28 21:00:20 +0000140 }
141 else
142 {
143 unlink($path.'/'.$filename);
144 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000145 }
146 }
Derek Jones269b9422008-01-28 21:00:20 +0000147 @closedir($current_dir);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000148
Derek Jones269b9422008-01-28 21:00:20 +0000149 if ($del_dir == TRUE AND $level > 0)
150 {
151 @rmdir($path);
152 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000153 }
154}
155
156// ------------------------------------------------------------------------
157
158/**
159 * Get Filenames
160 *
161 * Reads the specified directory and builds an array containing the filenames.
162 * Any sub-folders contained within the specified path are read as well.
163 *
164 * @access public
165 * @param string path to source
166 * @param bool whether to include the path as part of the filename
Derek Jones560efb32008-03-27 16:07:00 +0000167 * @param bool internal variable to determine recursion status - do not use in calls
Derek Allardd2df9bc2007-04-15 17:41:17 +0000168 * @return array
169 */
Derek Jones0b59f272008-05-13 04:22:33 +0000170if ( ! function_exists('get_filenames'))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000171{
Derek Jones560efb32008-03-27 16:07:00 +0000172 function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000173 {
Derek Jones14031d12008-05-05 18:29:43 +0000174 static $_filedata = array();
Derek Jonesf00992d2008-03-28 14:13:14 +0000175
Derek Jones269b9422008-01-28 21:00:20 +0000176 if ($fp = @opendir($source_dir))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000177 {
Derek Jonesf00992d2008-03-28 14:13:14 +0000178 // reset the array and make sure $source_dir has a trailing slash on the initial call
179 if ($_recursion === FALSE)
180 {
181 $_filedata = array();
Derek Jonesac7c81e2008-04-01 13:59:49 +0000182 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
Derek Jonesf00992d2008-03-28 14:13:14 +0000183 }
184
Derek Jones269b9422008-01-28 21:00:20 +0000185 while (FALSE !== ($file = readdir($fp)))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000186 {
Derek Allard73274992008-05-05 16:39:18 +0000187 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
Derek Jones269b9422008-01-28 21:00:20 +0000188 {
Derek Jonesac7c81e2008-04-01 13:59:49 +0000189 get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
Derek Jones269b9422008-01-28 21:00:20 +0000190 }
Derek Allard73274992008-05-05 16:39:18 +0000191 elseif (strncmp($file, '.', 1) !== 0)
Derek Jones269b9422008-01-28 21:00:20 +0000192 {
Derek Allardd2df9bc2007-04-15 17:41:17 +0000193
Derek Jones269b9422008-01-28 21:00:20 +0000194 $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
195 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000196 }
Derek Jones269b9422008-01-28 21:00:20 +0000197 return $_filedata;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000198 }
Derek Jonesfd971782008-02-20 18:39:18 +0000199 else
200 {
Derek Allardbca400f2008-02-21 22:19:55 +0000201 return FALSE;
Derek Jonesfd971782008-02-20 18:39:18 +0000202 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000203 }
204}
205
Derek Allarde4e603b2008-01-09 14:18:14 +0000206// --------------------------------------------------------------------
207
Derek Allard73274992008-05-05 16:39:18 +0000208/**
209 * Get Directory File Information
210 *
211 * Reads the specified directory and builds an array containing the filenames,
212 * filesize, dates, and permissions
213 *
214 * Any sub-folders contained within the specified path are read as well.
215 *
216 * @access public
217 * @param string path to source
218 * @param bool whether to include the path as part of the filename
219 * @param bool internal variable to determine recursion status - do not use in calls
220 * @return array
221 */
Derek Jones0b59f272008-05-13 04:22:33 +0000222if ( ! function_exists('get_dir_file_info'))
Derek Allard73274992008-05-05 16:39:18 +0000223{
224 function get_dir_file_info($source_dir, $include_path = FALSE, $_recursion = FALSE)
225 {
226 $_filedata = array();
227 $relative_path = $source_dir;
228
229 if ($fp = @opendir($source_dir))
230 {
231 // reset the array and make sure $source_dir has a trailing slash on the initial call
232 if ($_recursion === FALSE)
233 {
234 $_filedata = array();
235 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
236 }
237
238 while (FALSE !== ($file = readdir($fp)))
239 {
240 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
241 {
242 get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
243 }
244 elseif (strncmp($file, '.', 1) !== 0)
245 {
246 $_filedata[$file] = get_file_info($source_dir.$file);
247 $_filedata[$file]['relative_path'] = $relative_path;
248 }
249 }
250 return $_filedata;
251 }
252 else
253 {
254 return FALSE;
255 }
256 }
257}
258
259// --------------------------------------------------------------------
260
261/**
262* Get File Info
263*
264* Given a file and path, returns the name, path, size, date modified
265* Second parameter allows you to explicitly declare what information you want returned
266* Options are: name, server_path, size, date, readable, writable, executable, fileperms
267* Returns FALSE if the file cannot be found.
268*
Derek Allard993925b2008-08-21 12:43:31 +0000269* @access public
270* @param string path to file
271* @param mixed array or comma separated string of information returned
272* @return array
273*/
Derek Jones0b59f272008-05-13 04:22:33 +0000274if ( ! function_exists('get_file_info'))
Derek Allard73274992008-05-05 16:39:18 +0000275{
Derek Allard993925b2008-08-21 12:43:31 +0000276 function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
277 {
Derek Allard73274992008-05-05 16:39:18 +0000278
Derek Allard993925b2008-08-21 12:43:31 +0000279 if ( ! file_exists($file))
280 {
281 return FALSE;
282 }
Derek Allard73274992008-05-05 16:39:18 +0000283
Derek Allard993925b2008-08-21 12:43:31 +0000284 if (is_string($returned_values))
285 {
286 $returned_values = explode(',', $returned_values);
287 }
Derek Allard73274992008-05-05 16:39:18 +0000288
Derek Allard993925b2008-08-21 12:43:31 +0000289 foreach ($returned_values as $key)
290 {
291 switch ($key)
292 {
293 case 'name':
294 $fileinfo['name'] = substr(strrchr($file, '/'), 1);
295 break;
296 case 'server_path':
297 $fileinfo['server_path'] = $file;
298 break;
299 case 'size':
300 $fileinfo['size'] = filesize($file);
301 break;
302 case 'date':
303 $fileinfo['date'] = filectime($file);
304 break;
305 case 'readable':
306 $fileinfo['readable'] = is_readable($file);
307 break;
308 case 'writable':
309 // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
310 $fileinfo['writable'] = is_writable($file);
311 break;
312 case 'executable':
313 $fileinfo['executable'] = is_executable($file);
314 break;
315 case 'fileperms':
316 $fileinfo['fileperms'] = fileperms($file);
317 break;
318 }
319 }
Derek Allard73274992008-05-05 16:39:18 +0000320
Derek Allard993925b2008-08-21 12:43:31 +0000321 return $fileinfo;
322 }
Derek Allard73274992008-05-05 16:39:18 +0000323}
324
325// --------------------------------------------------------------------
326
327/**
328 * Get Mime by Extension
329 *
330 * Translates a file extension into a mime type based on config/mimes.php.
331 * Returns FALSE if it can't determine the type, or open the mime config file
332 *
333 * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
334 * It should NOT be trusted, and should certainly NOT be used for security
335 *
336 * @access public
337 * @param string path to file
338 * @return mixed
339 */
Derek Jones0b59f272008-05-13 04:22:33 +0000340if ( ! function_exists('get_mime_by_extension'))
Derek Allard73274992008-05-05 16:39:18 +0000341{
342 function get_mime_by_extension($file)
343 {
344 $extension = substr(strrchr($file, '.'), 1);
345
346 global $mimes;
347
Derek Jones0b59f272008-05-13 04:22:33 +0000348 if ( ! is_array($mimes))
Derek Allard73274992008-05-05 16:39:18 +0000349 {
Derek Jones0b59f272008-05-13 04:22:33 +0000350 if ( ! require_once(APPPATH.'config/mimes.php'))
Derek Allard73274992008-05-05 16:39:18 +0000351 {
352 return FALSE;
353 }
354 }
355
356 if (array_key_exists($extension, $mimes))
357 {
358 if (is_array($mimes[$extension]))
359 {
360 // Multiple mime types, just give the first one
361 return current($mimes[$extension]);
362 }
363 else
364 {
365 return $mimes[$extension];
366 }
367 }
368 else
369 {
370 return FALSE;
371 }
372 }
373}
374
Derek Jones94a21822008-05-08 15:00:28 +0000375// --------------------------------------------------------------------
376
377/**
378 * Symbolic Permissions
379 *
380 * Takes a numeric value representing a file's permissions and returns
381 * standard symbolic notation representing that value
382 *
383 * @access public
384 * @param int
385 * @return string
386 */
Derek Jones0b59f272008-05-13 04:22:33 +0000387if ( ! function_exists('symbolic_permissions'))
Derek Jones94a21822008-05-08 15:00:28 +0000388{
389 function symbolic_permissions($perms)
390 {
391 if (($perms & 0xC000) == 0xC000)
392 {
393 $symbolic = 's'; // Socket
394 }
395 elseif (($perms & 0xA000) == 0xA000)
396 {
397 $symbolic = 'l'; // Symbolic Link
398 }
399 elseif (($perms & 0x8000) == 0x8000)
400 {
401 $symbolic = '-'; // Regular
402 }
403 elseif (($perms & 0x6000) == 0x6000)
404 {
405 $symbolic = 'b'; // Block special
406 }
407 elseif (($perms & 0x4000) == 0x4000)
408 {
409 $symbolic = 'd'; // Directory
410 }
411 elseif (($perms & 0x2000) == 0x2000)
412 {
413 $symbolic = 'c'; // Character special
414 }
415 elseif (($perms & 0x1000) == 0x1000)
416 {
417 $symbolic = 'p'; // FIFO pipe
418 }
419 else
420 {
421 $symbolic = 'u'; // Unknown
422 }
423
424 // Owner
425 $symbolic .= (($perms & 0x0100) ? 'r' : '-');
426 $symbolic .= (($perms & 0x0080) ? 'w' : '-');
427 $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
428
429 // Group
430 $symbolic .= (($perms & 0x0020) ? 'r' : '-');
431 $symbolic .= (($perms & 0x0010) ? 'w' : '-');
432 $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
433
434 // World
435 $symbolic .= (($perms & 0x0004) ? 'r' : '-');
436 $symbolic .= (($perms & 0x0002) ? 'w' : '-');
437 $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
438
439 return $symbolic;
440 }
441}
442
443// --------------------------------------------------------------------
444
445/**
446 * Octal Permissions
447 *
448 * Takes a numeric value representing a file's permissions and returns
449 * a three character string representing the file's octal permissions
450 *
451 * @access public
452 * @param int
453 * @return string
454 */
Derek Jones0b59f272008-05-13 04:22:33 +0000455if ( ! function_exists('octal_permissions'))
Derek Jones94a21822008-05-08 15:00:28 +0000456{
457 function octal_permissions($perms)
458 {
459 return substr(sprintf('%o', $perms), -3);
460 }
461}
462
Derek Jones0b59f272008-05-13 04:22:33 +0000463
464/* End of file file_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000465/* Location: ./system/helpers/file_helper.php */