summaryrefslogtreecommitdiffstats
path: root/system/helpers/file_helper.php
diff options
context:
space:
mode:
authorDerek Jones <derek.jones@ellislab.com>2008-01-28 22:00:20 +0100
committerDerek Jones <derek.jones@ellislab.com>2008-01-28 22:00:20 +0100
commit269b942a2bf7b022795e591d9b0ad04526ee7e09 (patch)
treef465bb5a700d4cc5d72ca6e55e251640a46b869b /system/helpers/file_helper.php
parenta25530f6594c7ba45b3faa9537fda9f807069759 (diff)
added ability to "extend" helpers
* modified Loader to check for prefixed helpers in application/helpers folder * surrounded provided helper functions with if (! function_exists('foo')) conditionals so the user's helper functions take precedent.
Diffstat (limited to 'system/helpers/file_helper.php')
-rw-r--r--system/helpers/file_helper.php138
1 files changed, 75 insertions, 63 deletions
diff --git a/system/helpers/file_helper.php b/system/helpers/file_helper.php
index 868561b5a..bbf340930 100644
--- a/system/helpers/file_helper.php
+++ b/system/helpers/file_helper.php
@@ -36,35 +36,38 @@
* @param string path to file
* @return string
*/
-function read_file($file)
+if (! function_exists('read_file'))
{
- if ( ! file_exists($file))
+ function read_file($file)
{
- return FALSE;
- }
+ if ( ! file_exists($file))
+ {
+ return FALSE;
+ }
- if (function_exists('file_get_contents'))
- {
- return file_get_contents($file);
- }
+ if (function_exists('file_get_contents'))
+ {
+ return file_get_contents($file);
+ }
- if ( ! $fp = @fopen($file, 'rb'))
- {
- return FALSE;
- }
+ if ( ! $fp = @fopen($file, 'rb'))
+ {
+ return FALSE;
+ }
- flock($fp, LOCK_SH);
+ flock($fp, LOCK_SH);
- $data = '';
- if (filesize($file) > 0)
- {
- $data =& fread($fp, filesize($file));
- }
+ $data = '';
+ if (filesize($file) > 0)
+ {
+ $data =& fread($fp, filesize($file));
+ }
- flock($fp, LOCK_UN);
- fclose($fp);
+ flock($fp, LOCK_UN);
+ fclose($fp);
- return $data;
+ return $data;
+ }
}
// ------------------------------------------------------------------------
@@ -80,19 +83,22 @@ function read_file($file)
* @param string file data
* @return bool
*/
-function write_file($path, $data, $mode = 'wb')
+if (! function_exists('write_file'))
{
- if ( ! $fp = @fopen($path, $mode))
+ function write_file($path, $data, $mode = 'wb')
{
- return FALSE;
- }
+ if ( ! $fp = @fopen($path, $mode))
+ {
+ return FALSE;
+ }
- flock($fp, LOCK_EX);
- fwrite($fp, $data);
- flock($fp, LOCK_UN);
- fclose($fp);
+ flock($fp, LOCK_EX);
+ fwrite($fp, $data);
+ flock($fp, LOCK_UN);
+ fclose($fp);
- return TRUE;
+ return TRUE;
+ }
}
// ------------------------------------------------------------------------
@@ -110,34 +116,37 @@ function write_file($path, $data, $mode = 'wb')
* @param bool whether to delete any directories found in the path
* @return bool
*/
-function delete_files($path, $del_dir = FALSE, $level = 0)
-{
- // Trim the trailing slash
- $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
+if (! function_exists('delete_files'))
+{
+ function delete_files($path, $del_dir = FALSE, $level = 0)
+ {
+ // Trim the trailing slash
+ $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
- if ( ! $current_dir = @opendir($path))
- return;
+ if ( ! $current_dir = @opendir($path))
+ return;
- while(FALSE !== ($filename = @readdir($current_dir)))
- {
- if ($filename != "." and $filename != "..")
+ while(FALSE !== ($filename = @readdir($current_dir)))
{
- if (is_dir($path.'/'.$filename))
- {
- $level++;
- delete_files($path.'/'.$filename, $del_dir, $level);
- }
- else
+ if ($filename != "." and $filename != "..")
{
- unlink($path.'/'.$filename);
+ if (is_dir($path.'/'.$filename))
+ {
+ $level++;
+ delete_files($path.'/'.$filename, $del_dir, $level);
+ }
+ else
+ {
+ unlink($path.'/'.$filename);
+ }
}
}
- }
- @closedir($current_dir);
+ @closedir($current_dir);
- if ($del_dir == TRUE AND $level > 0)
- {
- @rmdir($path);
+ if ($del_dir == TRUE AND $level > 0)
+ {
+ @rmdir($path);
+ }
}
}
@@ -154,25 +163,28 @@ function delete_files($path, $del_dir = FALSE, $level = 0)
* @param bool whether to include the path as part of the filename
* @return array
*/
-function get_filenames($source_dir, $include_path = FALSE)
+if (! function_exists('get_filenames'))
{
- $_filedata = array();
-
- if ($fp = @opendir($source_dir))
+ function get_filenames($source_dir, $include_path = FALSE)
{
- while (FALSE !== ($file = readdir($fp)))
+ $_filedata = array();
+
+ if ($fp = @opendir($source_dir))
{
- if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.')
- {
- get_filenames($source_dir.$file."/", $include_path);
- }
- elseif (substr($file, 0, 1) != ".")
+ while (FALSE !== ($file = readdir($fp)))
{
+ if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.')
+ {
+ get_filenames($source_dir.$file."/", $include_path);
+ }
+ elseif (substr($file, 0, 1) != ".")
+ {
- $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
+ $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
+ }
}
+ return $_filedata;
}
- return $_filedata;
}
}