Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 1 | <?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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 24 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 25 | * @link http://codeigniter.com/user_guide/helpers/file_helpers.html
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 26 | */
|
| 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 Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 39 | if (! function_exists('read_file'))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 40 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 41 | function read_file($file)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 42 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 43 | if ( ! file_exists($file))
|
| 44 | {
|
| 45 | return FALSE;
|
| 46 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 47 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 48 | if (function_exists('file_get_contents'))
|
| 49 | {
|
| 50 | return file_get_contents($file);
|
| 51 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 52 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 53 | if ( ! $fp = @fopen($file, 'rb'))
|
| 54 | {
|
| 55 | return FALSE;
|
| 56 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 57 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 58 | flock($fp, LOCK_SH);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 59 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 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;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 70 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 86 | if (! function_exists('write_file'))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 87 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 88 | function write_file($path, $data, $mode = 'wb')
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 89 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 90 | if ( ! $fp = @fopen($path, $mode))
|
| 91 | {
|
| 92 | return FALSE;
|
| 93 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 94 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 95 | flock($fp, LOCK_EX);
|
| 96 | fwrite($fp, $data);
|
| 97 | flock($fp, LOCK_UN);
|
| 98 | fclose($fp);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 99 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 100 | return TRUE;
|
| 101 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 119 | if (! function_exists('delete_files'))
|
| 120 | {
|
| 121 | function delete_files($path, $del_dir = FALSE, $level = 0)
|
| 122 | {
|
| 123 | // Trim the trailing slash
|
| 124 | $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
|
Derek Jones | 1c8dd3a | 2008-01-29 20:20:00 +0000 | [diff] [blame] | 125 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 126 | if ( ! $current_dir = @opendir($path))
|
| 127 | return;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 128 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 129 | while(FALSE !== ($filename = @readdir($current_dir)))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 130 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 131 | if ($filename != "." and $filename != "..")
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 132 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 133 | if (is_dir($path.'/'.$filename))
|
| 134 | {
|
Derek Jones | 1c8dd3a | 2008-01-29 20:20:00 +0000 | [diff] [blame] | 135 | delete_files($path.'/'.$filename, $del_dir, $level + 1);
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 136 | }
|
| 137 | else
|
| 138 | {
|
| 139 | unlink($path.'/'.$filename);
|
| 140 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 141 | }
|
| 142 | }
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 143 | @closedir($current_dir);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 144 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 145 | if ($del_dir == TRUE AND $level > 0)
|
| 146 | {
|
| 147 | @rmdir($path);
|
| 148 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | // ------------------------------------------------------------------------
|
| 153 |
|
| 154 | /**
|
| 155 | * Get Filenames
|
| 156 | *
|
| 157 | * Reads the specified directory and builds an array containing the filenames.
|
| 158 | * Any sub-folders contained within the specified path are read as well.
|
| 159 | *
|
| 160 | * @access public
|
| 161 | * @param string path to source
|
| 162 | * @param bool whether to include the path as part of the filename
|
| 163 | * @return array
|
| 164 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 165 | if (! function_exists('get_filenames'))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 166 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 167 | function get_filenames($source_dir, $include_path = FALSE)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 168 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 169 | $_filedata = array();
|
| 170 |
|
| 171 | if ($fp = @opendir($source_dir))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 172 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 173 | while (FALSE !== ($file = readdir($fp)))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 174 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 175 | if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.')
|
| 176 | {
|
| 177 | get_filenames($source_dir.$file."/", $include_path);
|
| 178 | }
|
| 179 | elseif (substr($file, 0, 1) != ".")
|
| 180 | {
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 181 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 182 | $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
|
| 183 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 184 | }
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 185 | return $_filedata;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 186 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 187 | }
|
| 188 | }
|
| 189 |
|
Derek Allard | e4e603b | 2008-01-09 14:18:14 +0000 | [diff] [blame] | 190 | // --------------------------------------------------------------------
|
| 191 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 192 | ?> |