blob: 41ec2648ee5f76f445cd6af4d0bd4bb4eec45aca [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jonesfc395a12009-04-22 14:15:09 +00009 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @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
24 * @author ExpressionEngine Dev Team
Derek Jonesc37f0e12009-04-21 13:33:40 +000025 * @link http://codeigniter.com/user_guide/helpers/file_helpers.html
Derek Allard2067d1a2008-11-13 22:59:24 +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 */
39if ( ! function_exists('read_file'))
40{
41 function read_file($file)
42 {
43 if ( ! file_exists($file))
44 {
45 return FALSE;
46 }
47
48 if (function_exists('file_get_contents'))
49 {
50 return file_get_contents($file);
51 }
52
53 if ( ! $fp = @fopen($file, FOPEN_READ))
54 {
55 return FALSE;
56 }
57
58 flock($fp, LOCK_SH);
59
60 $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;
70 }
71}
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 */
86if ( ! function_exists('write_file'))
87{
88 function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
89 {
90 if ( ! $fp = @fopen($path, $mode))
91 {
92 return FALSE;
93 }
94
95 flock($fp, LOCK_EX);
96 fwrite($fp, $data);
97 flock($fp, LOCK_UN);
98 fclose($fp);
99
100 return TRUE;
101 }
102}
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 */
119if ( ! function_exists('delete_files'))
120{
121 function delete_files($path, $del_dir = FALSE, $level = 0)
122 {
123 // Trim the trailing slash
Derek Jonesc37f0e12009-04-21 13:33:40 +0000124 $path = rtrim($path, DIRECTORY_SEPARATOR);
125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 if ( ! $current_dir = @opendir($path))
127 return;
128
129 while(FALSE !== ($filename = @readdir($current_dir)))
130 {
131 if ($filename != "." and $filename != "..")
132 {
Derek Jonesc37f0e12009-04-21 13:33:40 +0000133 if (is_dir($path.DIRECTORY_SEPARATOR.$filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
135 // Ignore empty folders
136 if (substr($filename, 0, 1) != '.')
137 {
Derek Jonesc37f0e12009-04-21 13:33:40 +0000138 delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1);
139 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
141 else
142 {
Derek Jonesc37f0e12009-04-21 13:33:40 +0000143 unlink($path.DIRECTORY_SEPARATOR.$filename);
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145 }
146 }
147 @closedir($current_dir);
148
149 if ($del_dir == TRUE AND $level > 0)
150 {
151 @rmdir($path);
152 }
153 }
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
167 * @param bool internal variable to determine recursion status - do not use in calls
168 * @return array
169 */
170if ( ! function_exists('get_filenames'))
171{
172 function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
173 {
174 static $_filedata = array();
175
176 if ($fp = @opendir($source_dir))
177 {
178 // reset the array and make sure $source_dir has a trailing slash on the initial call
179 if ($_recursion === FALSE)
180 {
181 $_filedata = array();
182 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
183 }
184
185 while (FALSE !== ($file = readdir($fp)))
186 {
187 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
188 {
189 get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
190 }
191 elseif (strncmp($file, '.', 1) !== 0)
192 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
194 }
195 }
196 return $_filedata;
197 }
198 else
199 {
200 return FALSE;
201 }
202 }
203}
204
205// --------------------------------------------------------------------
206
207/**
208 * Get Directory File Information
209 *
210 * Reads the specified directory and builds an array containing the filenames,
211 * filesize, dates, and permissions
212 *
213 * Any sub-folders contained within the specified path are read as well.
214 *
215 * @access public
216 * @param string path to source
217 * @param bool whether to include the path as part of the filename
218 * @param bool internal variable to determine recursion status - do not use in calls
219 * @return array
220 */
221if ( ! function_exists('get_dir_file_info'))
222{
Derek Jones788b00f2009-11-27 18:00:20 +0000223 function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Derek Jones63dc27f2009-02-10 21:59:20 +0000225 static $_filedata = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 $relative_path = $source_dir;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 if ($fp = @opendir($source_dir))
229 {
230 // reset the array and make sure $source_dir has a trailing slash on the initial call
231 if ($_recursion === FALSE)
232 {
233 $_filedata = array();
234 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
235 }
236
Derek Jones788b00f2009-11-27 18:00:20 +0000237 // 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 +0000238 while (FALSE !== ($file = readdir($fp)))
239 {
Derek Jones788b00f2009-11-27 18:00:20 +0000240 if (@is_dir($source_dir.$file) AND strncmp($file, '.', 1) !== 0 AND $top_level_only === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Derek Jones788b00f2009-11-27 18:00:20 +0000242 get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
243 }
244 elseif (strncmp($file, '.', 1) !== 0)
245 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 $_filedata[$file] = get_file_info($source_dir.$file);
247 $_filedata[$file]['relative_path'] = $relative_path;
248 }
249 }
Derek Jones788b00f2009-11-27 18:00:20 +0000250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 return $_filedata;
252 }
253 else
254 {
255 return FALSE;
256 }
257 }
258}
259
260// --------------------------------------------------------------------
261
262/**
263* Get File Info
264*
265* Given a file and path, returns the name, path, size, date modified
266* Second parameter allows you to explicitly declare what information you want returned
267* Options are: name, server_path, size, date, readable, writable, executable, fileperms
268* Returns FALSE if the file cannot be found.
269*
270* @access public
Derek Jonesc37f0e12009-04-21 13:33:40 +0000271* @param string path to file
272* @param mixed array or comma separated string of information returned
Derek Allard2067d1a2008-11-13 22:59:24 +0000273* @return array
274*/
275if ( ! function_exists('get_file_info'))
276{
277 function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
278 {
279
280 if ( ! file_exists($file))
281 {
282 return FALSE;
283 }
284
285 if (is_string($returned_values))
286 {
287 $returned_values = explode(',', $returned_values);
288 }
289
290 foreach ($returned_values as $key)
291 {
292 switch ($key)
293 {
294 case 'name':
Derek Jonesc37f0e12009-04-21 13:33:40 +0000295 $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 break;
297 case 'server_path':
298 $fileinfo['server_path'] = $file;
299 break;
300 case 'size':
301 $fileinfo['size'] = filesize($file);
302 break;
303 case 'date':
304 $fileinfo['date'] = filectime($file);
305 break;
306 case 'readable':
307 $fileinfo['readable'] = is_readable($file);
308 break;
309 case 'writable':
310 // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
311 $fileinfo['writable'] = is_writable($file);
312 break;
313 case 'executable':
314 $fileinfo['executable'] = is_executable($file);
315 break;
316 case 'fileperms':
317 $fileinfo['fileperms'] = fileperms($file);
318 break;
319 }
320 }
321
322 return $fileinfo;
323 }
324}
325
326// --------------------------------------------------------------------
327
328/**
329 * Get Mime by Extension
330 *
331 * Translates a file extension into a mime type based on config/mimes.php.
332 * Returns FALSE if it can't determine the type, or open the mime config file
333 *
334 * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
335 * It should NOT be trusted, and should certainly NOT be used for security
336 *
337 * @access public
338 * @param string path to file
339 * @return mixed
340 */
341if ( ! function_exists('get_mime_by_extension'))
342{
343 function get_mime_by_extension($file)
344 {
Derek Allarde339ef52009-09-25 03:23:21 +0000345 $extension = strtolower(substr(strrchr($file, '.'), 1));
Derek Jonesc37f0e12009-04-21 13:33:40 +0000346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 global $mimes;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 if ( ! is_array($mimes))
350 {
351 if ( ! require_once(APPPATH.'config/mimes.php'))
352 {
353 return FALSE;
354 }
355 }
356
357 if (array_key_exists($extension, $mimes))
358 {
359 if (is_array($mimes[$extension]))
360 {
361 // Multiple mime types, just give the first one
362 return current($mimes[$extension]);
363 }
364 else
365 {
366 return $mimes[$extension];
367 }
368 }
369 else
370 {
371 return FALSE;
372 }
373 }
374}
375
376// --------------------------------------------------------------------
377
378/**
379 * Symbolic Permissions
380 *
381 * Takes a numeric value representing a file's permissions and returns
382 * standard symbolic notation representing that value
383 *
384 * @access public
385 * @param int
386 * @return string
387 */
388if ( ! function_exists('symbolic_permissions'))
389{
390 function symbolic_permissions($perms)
391 {
392 if (($perms & 0xC000) == 0xC000)
393 {
394 $symbolic = 's'; // Socket
395 }
396 elseif (($perms & 0xA000) == 0xA000)
397 {
398 $symbolic = 'l'; // Symbolic Link
399 }
400 elseif (($perms & 0x8000) == 0x8000)
401 {
402 $symbolic = '-'; // Regular
403 }
404 elseif (($perms & 0x6000) == 0x6000)
405 {
406 $symbolic = 'b'; // Block special
407 }
408 elseif (($perms & 0x4000) == 0x4000)
409 {
410 $symbolic = 'd'; // Directory
411 }
412 elseif (($perms & 0x2000) == 0x2000)
413 {
414 $symbolic = 'c'; // Character special
415 }
416 elseif (($perms & 0x1000) == 0x1000)
417 {
418 $symbolic = 'p'; // FIFO pipe
419 }
420 else
421 {
422 $symbolic = 'u'; // Unknown
423 }
424
425 // Owner
426 $symbolic .= (($perms & 0x0100) ? 'r' : '-');
427 $symbolic .= (($perms & 0x0080) ? 'w' : '-');
428 $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
429
430 // Group
431 $symbolic .= (($perms & 0x0020) ? 'r' : '-');
432 $symbolic .= (($perms & 0x0010) ? 'w' : '-');
433 $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
434
435 // World
436 $symbolic .= (($perms & 0x0004) ? 'r' : '-');
437 $symbolic .= (($perms & 0x0002) ? 'w' : '-');
438 $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
439
440 return $symbolic;
441 }
442}
443
444// --------------------------------------------------------------------
445
446/**
447 * Octal Permissions
448 *
449 * Takes a numeric value representing a file's permissions and returns
450 * a three character string representing the file's octal permissions
451 *
452 * @access public
453 * @param int
454 * @return string
455 */
456if ( ! function_exists('octal_permissions'))
457{
458 function octal_permissions($perms)
459 {
460 return substr(sprintf('%o', $perms), -3);
461 }
462}
463
464
465/* End of file file_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000466/* Location: ./system/helpers/file_helper.php */