admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Code Igniter File Helpers |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Helpers |
| 23 | * @category Helpers |
| 24 | * @author Rick Ellis |
| 25 | * @link http://www.codeigniter.com/user_guide/helpers/file_helpers.html |
| 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 | */ |
| 39 | function read_file($file) |
| 40 | { |
| 41 | if ( ! file_exists($file)) |
| 42 | { |
| 43 | return FALSE; |
| 44 | } |
admin | 78ce3cc | 2006-10-02 02:58:03 +0000 | [diff] [blame] | 45 | |
| 46 | if (function_exists('file_get_contents')) |
| 47 | { |
| 48 | return file_get_contents($file); |
| 49 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 50 | |
| 51 | if ( ! $fp = @fopen($file, 'rb')) |
| 52 | { |
| 53 | return FALSE; |
| 54 | } |
| 55 | |
| 56 | flock($fp, LOCK_SH); |
| 57 | |
| 58 | $data = ''; |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 59 | if (filesize($file) > 0) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 60 | { |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 61 | $data =& fread($fp, filesize($file)); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | flock($fp, LOCK_UN); |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 65 | fclose($fp); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 66 | |
| 67 | return $data; |
| 68 | } |
| 69 | |
| 70 | // ------------------------------------------------------------------------ |
| 71 | |
| 72 | /** |
| 73 | * Write File |
| 74 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 75 | * Writes data to the file specified in the path. |
admin | fafe28b | 2006-10-21 19:08:17 +0000 | [diff] [blame] | 76 | * Creates a new file if non-existent. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 77 | * |
| 78 | * @access public |
| 79 | * @param string path to file |
| 80 | * @param string file data |
| 81 | * @return bool |
| 82 | */ |
admin | 2ed76d5 | 2006-09-02 17:34:52 +0000 | [diff] [blame] | 83 | function write_file($path, $data, $mode = 'wb') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 84 | { |
admin | 2ed76d5 | 2006-09-02 17:34:52 +0000 | [diff] [blame] | 85 | if ( ! $fp = @fopen($path, $mode)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 86 | { |
| 87 | return FALSE; |
| 88 | } |
| 89 | |
| 90 | flock($fp, LOCK_EX); |
| 91 | fwrite($fp, $data); |
| 92 | flock($fp, LOCK_UN); |
| 93 | fclose($fp); |
| 94 | |
| 95 | return TRUE; |
| 96 | } |
| 97 | |
| 98 | // ------------------------------------------------------------------------ |
| 99 | |
| 100 | /** |
| 101 | * Delete Files |
| 102 | * |
| 103 | * Deletes all files contained in the supplied directory path. |
| 104 | * Files must be writable or owned by the system in order to be deleted. |
admin | fafe28b | 2006-10-21 19:08:17 +0000 | [diff] [blame] | 105 | * If the second parameter is set to TRUE, any directories contained |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 106 | * within the supplied base directory will be nuked as well. |
| 107 | * |
| 108 | * @access public |
| 109 | * @param string path to file |
| 110 | * @param bool whether to delete any directories found in the path |
| 111 | * @return bool |
| 112 | */ |
admin | 9fcc28a | 2006-10-21 17:49:47 +0000 | [diff] [blame] | 113 | function delete_files($path, $del_dir = FALSE, $level = 0) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 114 | { |
admin | 02855e0 | 2006-10-04 07:46:15 +0000 | [diff] [blame] | 115 | // Trim the trailing slash |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 116 | $path = preg_replace("|^(.+?)/*$|", "\\1", $path); |
| 117 | |
| 118 | if ( ! $current_dir = @opendir($path)) |
| 119 | return; |
| 120 | |
| 121 | while(FALSE !== ($filename = @readdir($current_dir))) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 122 | { |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 123 | if ($filename != "." and $filename != "..") |
| 124 | { |
| 125 | if (is_dir($path.'/'.$filename)) |
| 126 | { |
admin | 9fcc28a | 2006-10-21 17:49:47 +0000 | [diff] [blame] | 127 | $level++; |
| 128 | delete_files($path.'/'.$filename, $del_dir, $level); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | { |
| 132 | unlink($path.'/'.$filename); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | @closedir($current_dir); |
| 137 | |
admin | 9fcc28a | 2006-10-21 17:49:47 +0000 | [diff] [blame] | 138 | if ($del_dir == TRUE AND $level > 0) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 139 | { |
| 140 | @rmdir($path); |
| 141 | } |
| 142 | } |
| 143 | |
admin | c1e23ce | 2006-10-23 20:49:27 +0000 | [diff] [blame] | 144 | // ------------------------------------------------------------------------ |
| 145 | |
| 146 | /** |
| 147 | * Get Filenames |
| 148 | * |
| 149 | * Reads the specified directory and builds an array containing the filenames. |
| 150 | * Any sub-folders contained within the specified path are read as well. |
| 151 | * |
| 152 | * @access public |
| 153 | * @param string path to source |
| 154 | * @param bool whether to include the path as part of the filename |
| 155 | * @return array |
| 156 | */ |
| 157 | function get_filenames($source_dir, $include_path = FALSE) |
| 158 | { |
| 159 | static $_filedata = array(); |
| 160 | |
| 161 | if ($fp = @opendir($source_dir)) |
| 162 | { |
| 163 | while (FALSE !== ($file = readdir($fp))) |
| 164 | { |
| 165 | if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.') |
| 166 | { |
| 167 | get_filenames($source_dir.$file."/", $include_path); |
| 168 | } |
| 169 | elseif (substr($file, 0, 1) != ".") |
| 170 | { |
| 171 | |
| 172 | $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file; |
| 173 | } |
| 174 | } |
| 175 | return $_filedata; |
| 176 | } |
| 177 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 178 | |
| 179 | ?> |