blob: 32c0b2cdc9a130da1eb8c3c321569bfb5c397788 [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.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
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 */
39function read_file($file)
40{
41 if ( ! file_exists($file))
42 {
43 return FALSE;
44 }
45
46 if ( ! $fp = @fopen($file, 'rb'))
47 {
48 return FALSE;
49 }
50
51 flock($fp, LOCK_SH);
52
53 $data = '';
54 if (filesize($file) > 0)
55 {
56 $data = fread($fp, filesize($file));
57 }
58
59 flock($fp, LOCK_UN);
60 fclose($fp);
61
62 return $data;
63}
64
65// ------------------------------------------------------------------------
66
67/**
68 * Write File
69 *
70 * Writes data to the file specified in the path.
71 * Creats a new file if non-existant.
72 *
73 * @access public
74 * @param string path to file
75 * @param string file data
76 * @return bool
77 */
admin2ed76d52006-09-02 17:34:52 +000078function write_file($path, $data, $mode = 'wb')
adminb0dd10f2006-08-25 17:25:49 +000079{
admin2ed76d52006-09-02 17:34:52 +000080 if ( ! $fp = @fopen($path, $mode))
adminb0dd10f2006-08-25 17:25:49 +000081 {
82 return FALSE;
83 }
84
85 flock($fp, LOCK_EX);
86 fwrite($fp, $data);
87 flock($fp, LOCK_UN);
88 fclose($fp);
89
90 return TRUE;
91}
92
93// ------------------------------------------------------------------------
94
95/**
96 * Delete Files
97 *
98 * Deletes all files contained in the supplied directory path.
99 * Files must be writable or owned by the system in order to be deleted.
100 * If the second parameter is set to TRUE, any direcotries contained
101 * within the supplied base directory will be nuked as well.
102 *
103 * @access public
104 * @param string path to file
105 * @param bool whether to delete any directories found in the path
106 * @return bool
107 */
108function delete_files($path, $del_dir = FALSE)
109{
110 // Trim the trailing slahs
111 $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
112
113 if ( ! $current_dir = @opendir($path))
114 return;
115
116 while(FALSE !== ($filename = @readdir($current_dir)))
117 {
118 if ($filename != "." and $filename != "..")
119 {
120 if (is_dir($path.'/'.$filename))
121 {
122 delete_files($path.'/'.$filename, $del_dir);
123 }
124 else
125 {
126 unlink($path.'/'.$filename);
127 }
128 }
129 }
130 @closedir($current_dir);
131
132 if ($del_dir == TRUE)
133 {
134 @rmdir($path);
135 }
136}
137
138
139?>