blob: bbf3409301f30119996860c2599c5766d0e1d5e9 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +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
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, 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 Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/helpers/file_helpers.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 Jones269b9422008-01-28 21:00:20 +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 Jones269b9422008-01-28 21:00:20 +000043 if ( ! file_exists($file))
44 {
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 Jones269b9422008-01-28 21:00:20 +000053 if ( ! $fp = @fopen($file, 'rb'))
54 {
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 Jones269b9422008-01-28 21:00:20 +000086if (! function_exists('write_file'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000087{
Derek Jones269b9422008-01-28 21:00:20 +000088 function write_file($path, $data, $mode = 'wb')
Derek Allardd2df9bc2007-04-15 17:41:17 +000089 {
Derek Jones269b9422008-01-28 21:00:20 +000090 if ( ! $fp = @fopen($path, $mode))
91 {
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 Jones269b9422008-01-28 21:00:20 +0000119if (! 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 Allardd2df9bc2007-04-15 17:41:17 +0000125
Derek Jones269b9422008-01-28 21:00:20 +0000126 if ( ! $current_dir = @opendir($path))
127 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 {
135 $level++;
136 delete_files($path.'/'.$filename, $del_dir, $level);
137 }
138 else
139 {
140 unlink($path.'/'.$filename);
141 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000142 }
143 }
Derek Jones269b9422008-01-28 21:00:20 +0000144 @closedir($current_dir);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000145
Derek Jones269b9422008-01-28 21:00:20 +0000146 if ($del_dir == TRUE AND $level > 0)
147 {
148 @rmdir($path);
149 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000150 }
151}
152
153// ------------------------------------------------------------------------
154
155/**
156 * Get Filenames
157 *
158 * Reads the specified directory and builds an array containing the filenames.
159 * Any sub-folders contained within the specified path are read as well.
160 *
161 * @access public
162 * @param string path to source
163 * @param bool whether to include the path as part of the filename
164 * @return array
165 */
Derek Jones269b9422008-01-28 21:00:20 +0000166if (! function_exists('get_filenames'))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000167{
Derek Jones269b9422008-01-28 21:00:20 +0000168 function get_filenames($source_dir, $include_path = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000169 {
Derek Jones269b9422008-01-28 21:00:20 +0000170 $_filedata = array();
171
172 if ($fp = @opendir($source_dir))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000173 {
Derek Jones269b9422008-01-28 21:00:20 +0000174 while (FALSE !== ($file = readdir($fp)))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000175 {
Derek Jones269b9422008-01-28 21:00:20 +0000176 if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.')
177 {
178 get_filenames($source_dir.$file."/", $include_path);
179 }
180 elseif (substr($file, 0, 1) != ".")
181 {
Derek Allardd2df9bc2007-04-15 17:41:17 +0000182
Derek Jones269b9422008-01-28 21:00:20 +0000183 $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
184 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000185 }
Derek Jones269b9422008-01-28 21:00:20 +0000186 return $_filedata;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000187 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000188 }
189}
190
Derek Allarde4e603b2008-01-09 14:18:14 +0000191// --------------------------------------------------------------------
192
adminb0dd10f2006-08-25 17:25:49 +0000193?>