blob: c0f52568894dd219c76f1c8741149994e52f04b8 [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 }
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 = '';
59 if (filesize($file) > 0)
60 {
admin78ce3cc2006-10-02 02:58:03 +000061 $data =& fread($fp, filesize($file));
adminb0dd10f2006-08-25 17:25:49 +000062 }
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 * Creats a new file if non-existant.
77 *
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.
105 * If the second parameter is set to TRUE, any direcotries 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)
114{
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)))
122 {
123 if ($filename != "." and $filename != "..")
124 {
125 if (is_dir($path.'/'.$filename))
126 {
127 delete_files($path.'/'.$filename, $del_dir);
128 }
129 else
130 {
131 unlink($path.'/'.$filename);
132 }
133 }
134 }
135 @closedir($current_dir);
136
137 if ($del_dir == TRUE)
138 {
139 @rmdir($path);
140 }
141}
142
143
144?>