blob: 4da62ade6d71335dd023b9548c41bbd3ae257dd8 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
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 */
39function read_file($file)
40{
41 if ( ! file_exists($file))
42 {
43 return FALSE;
44 }
admin78ce3cc2006-10-02 02:58:03 +000045
46 if (function_exists('file_get_contents'))
47 {
48 return file_get_contents($file);
49 }
adminb0dd10f2006-08-25 17:25:49 +000050
51 if ( ! $fp = @fopen($file, 'rb'))
52 {
53 return FALSE;
54 }
55
56 flock($fp, LOCK_SH);
57
58 $data = '';
admine334c472006-10-21 19:44:22 +000059 if (filesize($file) > 0)
adminb0dd10f2006-08-25 17:25:49 +000060 {
admine334c472006-10-21 19:44:22 +000061 $data =& fread($fp, filesize($file));
adminb0dd10f2006-08-25 17:25:49 +000062 }
63
64 flock($fp, LOCK_UN);
admine334c472006-10-21 19:44:22 +000065 fclose($fp);
adminb0dd10f2006-08-25 17:25:49 +000066
67 return $data;
68}
69
70// ------------------------------------------------------------------------
71
72/**
73 * Write File
74 *
admine334c472006-10-21 19:44:22 +000075 * Writes data to the file specified in the path.
adminfafe28b2006-10-21 19:08:17 +000076 * Creates a new file if non-existent.
adminb0dd10f2006-08-25 17:25:49 +000077 *
78 * @access public
79 * @param string path to file
80 * @param string file data
81 * @return bool
82 */
admin2ed76d52006-09-02 17:34:52 +000083function write_file($path, $data, $mode = 'wb')
adminb0dd10f2006-08-25 17:25:49 +000084{
admin2ed76d52006-09-02 17:34:52 +000085 if ( ! $fp = @fopen($path, $mode))
adminb0dd10f2006-08-25 17:25:49 +000086 {
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.
adminfafe28b2006-10-21 19:08:17 +0000105 * If the second parameter is set to TRUE, any directories contained
adminb0dd10f2006-08-25 17:25:49 +0000106 * 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 */
admin9fcc28a2006-10-21 17:49:47 +0000113function delete_files($path, $del_dir = FALSE, $level = 0)
adminb0dd10f2006-08-25 17:25:49 +0000114{
admin02855e02006-10-04 07:46:15 +0000115 // Trim the trailing slash
adminb0dd10f2006-08-25 17:25:49 +0000116 $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
117
118 if ( ! $current_dir = @opendir($path))
119 return;
120
121 while(FALSE !== ($filename = @readdir($current_dir)))
admine334c472006-10-21 19:44:22 +0000122 {
adminb0dd10f2006-08-25 17:25:49 +0000123 if ($filename != "." and $filename != "..")
124 {
125 if (is_dir($path.'/'.$filename))
126 {
admin9fcc28a2006-10-21 17:49:47 +0000127 $level++;
128 delete_files($path.'/'.$filename, $del_dir, $level);
adminb0dd10f2006-08-25 17:25:49 +0000129 }
130 else
131 {
132 unlink($path.'/'.$filename);
133 }
134 }
135 }
136 @closedir($current_dir);
137
admin9fcc28a2006-10-21 17:49:47 +0000138 if ($del_dir == TRUE AND $level > 0)
adminb0dd10f2006-08-25 17:25:49 +0000139 {
140 @rmdir($path);
141 }
142}
143
adminc1e23ce2006-10-23 20:49:27 +0000144// ------------------------------------------------------------------------
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{
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}
adminb0dd10f2006-08-25 17:25:49 +0000178
179?>