1 |
6daefa8c
|
Petr Lukašík
|
<?php
|
2 |
|
|
/* SVN FILE: $Id: file.php 4409 2007-02-02 13:20:59Z phpnut $ */
|
3 |
|
|
/**
|
4 |
|
|
* Convenience class for reading, writing and appending to files.
|
5 |
|
|
*
|
6 |
|
|
* PHP versions 4 and 5
|
7 |
|
|
*
|
8 |
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
9 |
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
10 |
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
11 |
|
|
* Las Vegas, Nevada 89104
|
12 |
|
|
*
|
13 |
|
|
* Licensed under The MIT License
|
14 |
|
|
* Redistributions of files must retain the above copyright notice.
|
15 |
|
|
*
|
16 |
|
|
* @filesource
|
17 |
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
18 |
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
19 |
|
|
* @package cake
|
20 |
|
|
* @subpackage cake.cake.libs
|
21 |
|
|
* @since CakePHP(tm) v 0.2.9
|
22 |
|
|
* @version $Revision: 4409 $
|
23 |
|
|
* @modifiedby $LastChangedBy: phpnut $
|
24 |
|
|
* @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
|
25 |
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
26 |
|
|
*/
|
27 |
|
|
/**
|
28 |
|
|
* Included libraries.
|
29 |
|
|
*
|
30 |
|
|
*/
|
31 |
|
|
if (!class_exists('Object')) {
|
32 |
|
|
uses ('object');
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
if (!class_exists('Folder')) {
|
36 |
|
|
uses ('folder');
|
37 |
|
|
}
|
38 |
|
|
/**
|
39 |
|
|
* Convenience class for reading, writing and appending to files.
|
40 |
|
|
*
|
41 |
|
|
* @package cake
|
42 |
|
|
* @subpackage cake.cake.libs
|
43 |
|
|
*/
|
44 |
|
|
class File extends Object{
|
45 |
|
|
/**
|
46 |
|
|
* Folder of the File
|
47 |
|
|
*
|
48 |
|
|
* @var Folder
|
49 |
|
|
*/
|
50 |
|
|
var $folder = null;
|
51 |
|
|
/**
|
52 |
|
|
* Filename
|
53 |
|
|
*
|
54 |
|
|
* @var string
|
55 |
|
|
*/
|
56 |
|
|
var $name = null;
|
57 |
|
|
/**
|
58 |
|
|
* Constructor
|
59 |
|
|
*
|
60 |
|
|
* @param string $path
|
61 |
|
|
* @param boolean $create Create file if it does not exist
|
62 |
|
|
* @return File
|
63 |
|
|
*/
|
64 |
|
|
function __construct($path, $create = false) {
|
65 |
|
|
parent::__construct();
|
66 |
|
|
$this->folder = new Folder(dirname($path), $create);
|
67 |
|
|
$this->name = basename($path);
|
68 |
|
|
if (!$this->exists()) {
|
69 |
|
|
if ($create === true) {
|
70 |
|
|
if (!$this->create()) {
|
71 |
|
|
return false;
|
72 |
|
|
}
|
73 |
|
|
} else {
|
74 |
|
|
return false;
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
/**
|
79 |
|
|
* Return the contents of this File as a string.
|
80 |
|
|
*
|
81 |
|
|
* @return string Contents
|
82 |
|
|
*/
|
83 |
|
|
function read() {
|
84 |
|
|
$contents = file_get_contents($this->getFullPath());
|
85 |
|
|
return $contents;
|
86 |
|
|
}
|
87 |
|
|
/**
|
88 |
|
|
* Append given data string to this File.
|
89 |
|
|
*
|
90 |
|
|
* @param string $data Data to write
|
91 |
|
|
* @return boolean Success
|
92 |
|
|
*/
|
93 |
|
|
function append($data) {
|
94 |
|
|
return $this->write($data, 'a');
|
95 |
|
|
}
|
96 |
|
|
/**
|
97 |
|
|
* Write given data to this File.
|
98 |
|
|
*
|
99 |
|
|
* @param string $data Data to write to this File.
|
100 |
|
|
* @param string $mode Mode of writing. {@link http://php.net/fwrite See fwrite()}.
|
101 |
|
|
* @return boolean Success
|
102 |
|
|
*/
|
103 |
|
|
function write($data, $mode = 'w') {
|
104 |
|
|
$file = $this->getFullPath();
|
105 |
|
|
if (!($handle = fopen($file, $mode))) {
|
106 |
|
|
print ("[File] Could not open $file with mode $mode!");
|
107 |
|
|
return false;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
if (!fwrite($handle, $data)) {
|
111 |
|
|
return false;
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
if (!fclose($handle)) {
|
115 |
|
|
return false;
|
116 |
|
|
}
|
117 |
|
|
return true;
|
118 |
|
|
}
|
119 |
|
|
/**
|
120 |
|
|
* Get md5 Checksum of file with previous check of Filesize
|
121 |
|
|
*
|
122 |
|
|
* @param string $force Data to write to this File.
|
123 |
|
|
* @return string md5 Checksum {@link http://php.net/md5_file See md5_file()}
|
124 |
|
|
*/
|
125 |
|
|
function getMd5($force = false) {
|
126 |
|
|
$md5 = '';
|
127 |
|
|
if ($force == true || $this->getSize(false) < MAX_MD5SIZE) {
|
128 |
|
|
$md5 = md5_file($this->getFullPath());
|
129 |
|
|
}
|
130 |
|
|
return $md5;
|
131 |
|
|
}
|
132 |
|
|
/**
|
133 |
|
|
* Returns the Filesize, either in bytes or in human-readable format.
|
134 |
|
|
*
|
135 |
|
|
* @param boolean $humanReadeble Data to write to this File.
|
136 |
|
|
* @return string|int filesize as int or as a human-readable string
|
137 |
|
|
*/
|
138 |
|
|
function getSize() {
|
139 |
|
|
$size = filesize($this->getFullPath());
|
140 |
|
|
return $size;
|
141 |
|
|
}
|
142 |
|
|
/**
|
143 |
|
|
* Returns the File extension.
|
144 |
|
|
*
|
145 |
|
|
* @return string The Fileextension
|
146 |
|
|
*/
|
147 |
|
|
function getExt() {
|
148 |
|
|
$ext = '';
|
149 |
|
|
$parts = explode('.', $this->getName());
|
150 |
|
|
|
151 |
|
|
if (count($parts) > 1) {
|
152 |
|
|
$ext = array_pop($parts);
|
153 |
|
|
} else {
|
154 |
|
|
$ext = '';
|
155 |
|
|
}
|
156 |
|
|
return $ext;
|
157 |
|
|
}
|
158 |
|
|
/**
|
159 |
|
|
* Returns the filename.
|
160 |
|
|
*
|
161 |
|
|
* @return string The Filename
|
162 |
|
|
*/
|
163 |
|
|
function getName() {
|
164 |
|
|
return $this->name;
|
165 |
|
|
}
|
166 |
|
|
/**
|
167 |
|
|
* Returns the File's owner.
|
168 |
|
|
*
|
169 |
|
|
* @return int the Fileowner
|
170 |
|
|
*/
|
171 |
|
|
function getOwner() {
|
172 |
|
|
$fileowner = fileowner($this->getFullPath());
|
173 |
|
|
return $fileowner;
|
174 |
|
|
}
|
175 |
|
|
/**
|
176 |
|
|
* Returns the File group.
|
177 |
|
|
*
|
178 |
|
|
* @return int the Filegroup
|
179 |
|
|
*/
|
180 |
|
|
function getGroup() {
|
181 |
|
|
$filegroup = filegroup($this->getFullPath());
|
182 |
|
|
return $filegroup;
|
183 |
|
|
}
|
184 |
|
|
/**
|
185 |
|
|
* Creates the File.
|
186 |
|
|
*
|
187 |
|
|
* @return boolean Success
|
188 |
|
|
*/
|
189 |
|
|
function create() {
|
190 |
|
|
$dir = $this->folder->pwd();
|
191 |
|
|
|
192 |
|
|
if (file_exists($dir) && is_dir($dir) && is_writable($dir) && !$this->exists()) {
|
193 |
|
|
if (!touch($this->getFullPath())) {
|
194 |
|
|
print ('[File] Could not create '.$this->getName().'!');
|
195 |
|
|
return false;
|
196 |
|
|
} else {
|
197 |
|
|
return true;
|
198 |
|
|
}
|
199 |
|
|
} else {
|
200 |
|
|
print ('[File] Could not create '.$this->getName().'!');
|
201 |
|
|
return false;
|
202 |
|
|
}
|
203 |
|
|
}
|
204 |
|
|
/**
|
205 |
|
|
* Returns true if the File exists.
|
206 |
|
|
*
|
207 |
|
|
* @return boolean
|
208 |
|
|
*/
|
209 |
|
|
function exists() {
|
210 |
|
|
$exists = file_exists($this->getFullPath());
|
211 |
|
|
return $exists;
|
212 |
|
|
}
|
213 |
|
|
/**
|
214 |
|
|
* Deletes the File.
|
215 |
|
|
*
|
216 |
|
|
* @return boolean
|
217 |
|
|
*/
|
218 |
|
|
function delete() {
|
219 |
|
|
$unlink = unlink($this->getFullPath());
|
220 |
|
|
return $unlink;
|
221 |
|
|
}
|
222 |
|
|
/**
|
223 |
|
|
* Returns true if the File is writable.
|
224 |
|
|
*
|
225 |
|
|
* @return boolean
|
226 |
|
|
*/
|
227 |
|
|
function writable() {
|
228 |
|
|
$writable = is_writable($this->getFullPath());
|
229 |
|
|
return $writable;
|
230 |
|
|
}
|
231 |
|
|
/**
|
232 |
|
|
* Returns true if the File is executable.
|
233 |
|
|
*
|
234 |
|
|
* @return boolean
|
235 |
|
|
*/
|
236 |
|
|
function executable() {
|
237 |
|
|
$executable = is_executable($this->getFullPath());
|
238 |
|
|
return $executable;
|
239 |
|
|
}
|
240 |
|
|
/**
|
241 |
|
|
* Returns true if the File is readable.
|
242 |
|
|
*
|
243 |
|
|
* @return boolean
|
244 |
|
|
*/
|
245 |
|
|
function readable() {
|
246 |
|
|
$readable = is_readable($this->getFullPath());
|
247 |
|
|
return $readable;
|
248 |
|
|
}
|
249 |
|
|
/**
|
250 |
|
|
* Returns last access time.
|
251 |
|
|
*
|
252 |
|
|
* @return int timestamp
|
253 |
|
|
*/
|
254 |
|
|
function lastAccess() {
|
255 |
|
|
$fileatime = fileatime($this->getFullPath());
|
256 |
|
|
return $fileatime;
|
257 |
|
|
}
|
258 |
|
|
/**
|
259 |
|
|
* Returns last modified time.
|
260 |
|
|
*
|
261 |
|
|
* @return int timestamp
|
262 |
|
|
*/
|
263 |
|
|
function lastChange() {
|
264 |
|
|
$filemtime = filemtime($this->getFullPath());
|
265 |
|
|
return $filemtime;
|
266 |
|
|
}
|
267 |
|
|
/**
|
268 |
|
|
* Returns the current folder.
|
269 |
|
|
*
|
270 |
|
|
* @return Folder
|
271 |
|
|
*/
|
272 |
|
|
function getFolder() {
|
273 |
|
|
return $this->folder;
|
274 |
|
|
}
|
275 |
|
|
/**
|
276 |
|
|
* Returns the "chmod" (permissions) of the File.
|
277 |
|
|
*
|
278 |
|
|
* @return string
|
279 |
|
|
*/
|
280 |
|
|
function getChmod() {
|
281 |
|
|
$substr = substr(sprintf('%o', fileperms($this->getFullPath())), -4);
|
282 |
|
|
return $substr;
|
283 |
|
|
}
|
284 |
|
|
/**
|
285 |
|
|
* Returns the full path of the File.
|
286 |
|
|
*
|
287 |
|
|
* @return string
|
288 |
|
|
*/
|
289 |
|
|
function getFullPath() {
|
290 |
|
|
return $this->folder->slashTerm($this->folder->pwd()) . $this->getName();
|
291 |
|
|
}
|
292 |
|
|
}
|
293 |
|
|
?>
|