blob: 83ef6a066b03cd481613d66ea9b1ba024b9b6d50 [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
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000011 * @link http://www.codeigniter.com
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
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 */
39function read_file($file)
40{
41 if ( ! file_exists($file))
42 {
43 return FALSE;
44 }
45
46 if (function_exists('file_get_contents'))
47 {
48 return file_get_contents($file);
49 }
50
51 if ( ! $fp = @fopen($file, 'rb'))
52 {
53 return FALSE;
54 }
55
56 flock($fp, LOCK_SH);
57
58 $data = '';
59 if (filesize($file) > 0)
60 {
61 $data =& fread($fp, filesize($file));
62 }
63
64 flock($fp, LOCK_UN);
65 fclose($fp);
66
67 return $data;
68}
69
70// ------------------------------------------------------------------------
71
72/**
73 * Write File
74 *
75 * Writes data to the file specified in the path.
76 * Creates a new file if non-existent.
77 *
78 * @access public
79 * @param string path to file
80 * @param string file data
81 * @return bool
82 */
83function write_file($path, $data, $mode = 'wb')
84{
85 if ( ! $fp = @fopen($path, $mode))
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.
105 * If the second parameter is set to TRUE, any directories contained
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 */
113function delete_files($path, $del_dir = FALSE, $level = 0)
114{
115 // Trim the trailing slash
116 $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
117
118 if ( ! $current_dir = @opendir($path))
119 return;
120
121 while(FALSE !== ($filename = @readdir($current_dir)))
122 {
123 if ($filename != "." and $filename != "..")
124 {
125 if (is_dir($path.'/'.$filename))
126 {
127 $level++;
128 delete_files($path.'/'.$filename, $del_dir, $level);
129 }
130 else
131 {
132 unlink($path.'/'.$filename);
133 }
134 }
135 }
136 @closedir($current_dir);
137
138 if ($del_dir == TRUE AND $level > 0)
139 {
140 @rmdir($path);
141 }
142}
143
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 */
157function get_filenames($source_dir, $include_path = FALSE)
158{
Derek Allarda0fa90b2008-01-01 15:59:47 +0000159 $_filedata = array();
Derek Allardd2df9bc2007-04-15 17:41:17 +0000160
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}
178
Derek Allarde4e603b2008-01-09 14:18:14 +0000179// --------------------------------------------------------------------
180
181/**
182 * Tests for file writability
183 *
184 * is_writable() returns TRUE on Windows servers
185 * when you really can't write to the file
186 * as the OS reports to PHP as FALSE only if the
187 * read-only attribute is marked. Ugh?
188 *
189 * @access private
190 * @return void
191 */
192function is_really_writable($file)
193{
194 if (is_dir($file))
195 {
196 $file = rtrim($file, '/').'/'.md5(rand(1,100));
197
198 if (($fp = @fopen($file, 'ab')) === FALSE)
199 {
200 return FALSE;
201 }
202
203 @chmod($file, 0777);
204 @unlink($file);
205 }
206 elseif (($fp = @fopen($file, 'ab')) === FALSE)
207 {
208 return FALSE;
209 }
210
211 fclose($fp);
212 return TRUE;
213}
adminb0dd10f2006-08-25 17:25:49 +0000214?>