blob: 0005c2a24e1753766a0dae039e30f60f25f4044c [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 Jones1c8dd3a2008-01-29 20:20:00 +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 {
Derek Jones1c8dd3a2008-01-29 20:20:00 +0000135 delete_files($path.'/'.$filename, $del_dir, $level + 1);
Derek Jones269b9422008-01-28 21:00:20 +0000136 }
137 else
138 {
139 unlink($path.'/'.$filename);
140 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000141 }
142 }
Derek Jones269b9422008-01-28 21:00:20 +0000143 @closedir($current_dir);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000144
Derek Jones269b9422008-01-28 21:00:20 +0000145 if ($del_dir == TRUE AND $level > 0)
146 {
147 @rmdir($path);
148 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000149 }
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 Jones269b9422008-01-28 21:00:20 +0000165if (! function_exists('get_filenames'))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000166{
Derek Jones269b9422008-01-28 21:00:20 +0000167 function get_filenames($source_dir, $include_path = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000168 {
Derek Jones269b9422008-01-28 21:00:20 +0000169 $_filedata = array();
170
171 if ($fp = @opendir($source_dir))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000172 {
Derek Jones269b9422008-01-28 21:00:20 +0000173 while (FALSE !== ($file = readdir($fp)))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000174 {
Derek Jones269b9422008-01-28 21:00:20 +0000175 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 Allardd2df9bc2007-04-15 17:41:17 +0000181
Derek Jones269b9422008-01-28 21:00:20 +0000182 $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
183 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000184 }
Derek Jones269b9422008-01-28 21:00:20 +0000185 return $_filedata;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000186 }
Derek Jonesfd971782008-02-20 18:39:18 +0000187 else
188 {
189 return $FALSE;
190 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000191 }
192}
193
Derek Allarde4e603b2008-01-09 14:18:14 +0000194// --------------------------------------------------------------------
195
adminb0dd10f2006-08-25 17:25:49 +0000196?>