-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.php
More file actions
442 lines (408 loc) · 8.61 KB
/
FileUtils.php
File metadata and controls
442 lines (408 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
/**
* M PHP Framework
*
* Misc utilities related to files/folders manipulations
*
* @package M
* @subpackage FileUtils
* @author Arnaud Sellenet <demental at github>
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @version 0.1
*/
/**
*
* Misc utilities related to files/folders manipulations
* This class can be instanciated for some operations, useful if you want to work
* on a specific set of files.
*
*/
class FileUtils
{
/**
*
* Pattern regular expression
*
* @var string
* @access public
*/
public $_pattern='^\.$|^\.\.$';
/**
*
* Constructor
*
* @param $file string File URL
* @return string
*/
function __construct($file)
{
if(is_file($file)) {
$this->file = $file;
} else {
$this->file = self::ensure_trailing_slash($file);
}
}
/**
*
* Add extension to exclude pattern
*
* @param $ext string Extension to add
*/
public function addExcludeExtension($ext)
{
$this->addExcludePattern('.+\.'.$ext.'$');
}
/**
*
* Add exclude pattern
*
* @param $pattern string Pattern to exclude
*/
public function addExcludePattern($pattern)
{
$this->_pattern .= '|'.$pattern;
}
/**
*
* Check if file is an image
*
* @param $file string File URL
* @return boolean
*/
public static function is_Image($file)
{
return in_array(fileUtils::getFileExtension($file),array('jpg','jpeg','gif','png'));
}
/**
*
* Get the file extension
*
* @param $file string File name/URL
* @return extension string
*/
function getFileExtension($file)
{
$nom=explode(".",$file);
$ext=$nom[count($nom)-1];
return strtolower($ext);
}
/**
*
* Get files older than a given date
*
* @param $time unix timestamp
* @param $absolute boolean
* @return Files array
*/
public function getFilesOlderThan($time,$absolute=false)
{
$t = time()-$time;
foreach($this->getContents() as $file) {
if(filemtime($this->file.$file)<$t) {
$out[]=$absolute?$this->file.$file:$file;
}
}
return $out;
}
/**
*
* Delete files older than a given date
*
* @param $time unix timestamp
*/
public function deleteFilesOlderThan($time)
{
foreach($this->getFilesOlderThan($time) as $file) {
unlink($this->file.$file);
}
}
/**
* Check if a file exists in the include path
*
* @version 1.2.1
* @author Aidan Lister <aidan@php.net>
* @link http://aidanlister.com/repos/v/function.file_exists_incpath.php
* @param string $file Name of the file to look for
* @return mixed The full path if file exists, FALSE if it does not
*/
public static function file_exists_incpath ($file)
{
if(strpos($file,'/') === 0) {
return file_exists($file);
}
$paths = explode(PATH_SEPARATOR, get_include_path());
foreach ($paths as $path) {
// Formulate the absolute path
$fullpath = $path . '/' . $file;
// Check it
if (file_exists($fullpath)) {
return $fullpath;
}
}
return false;
}
/**
*
* Get Files list of a given folder
*
* @access public
* @static
* @param $folder string Source folder
* @return Files list array
*/
public static function getFiles($folder)
{
if(!is_dir($folder)) {
return array();
}
$out = array();
if ($dh = @opendir($folder)) {
while (($file = readdir($dh)) !== false)
{
if(!is_file($folder.$file)) continue;
$out[]=$file;
}
}
return $out;
}
/**
*
* Get Subfolders list of a given folder
*
* @access public
* @static
* @param $folder string Source folder
* @return Files list array
*/
public static function getFolders($folder)
{
if(!is_dir($folder)) {
return array();
}
$out = array();
if ($dh = @opendir($folder)) {
while (($file = readdir($dh)) !== false)
{
if(is_file($folder.$file)) continue;
$out[]=$file;
}
}
return $out;
}
/**
*
* Get Files list of a given folder with extension filtering capabilities
*
* @access public
* @static
* @param $folder string Source folder
* @param $extensionfilter string Extension to filter
* @param $recursive bool recursively scan folders (equivalent to ls -R)
* @return Files list array
*/
public static function getAllFiles($folder,$extensionfilter='',$recursive = true)
{
if(!is_array($extensionfilter)) {
$extensionfilter = array($extensionfilter);
}
$folder = self::ensure_trailing_slash($folder);
if(!is_dir($folder)) {
return array();
}
$out = array();
if ($dh = @opendir($folder)) {
while (($file = readdir($dh)) !== false)
{
if(self::is_dot_file($file)) continue;
if(is_dir($folder.$file) && $recursive) {
$out = array_merge($out,self::getAllFiles($folder.$file,$extensionfilter));
} elseif(!is_file($folder.$file)) {
continue;
} else {
if(empty($extensionfilter[0]) || preg_match('`\.'.implode('|',$extensionfilter).'`',$file)) {
$out[]=$folder.$file;
}
}
}
sort($out);
return $out;
}
}
/**
*
* Get Files list of a given folder with extension filtering capabilities
* PLUS folders
* @access public
* @static
* @param $folder string Source folder
* @param $extensionfilter string Extension to filter
* @return Files list array
*/
public static function getAll($folder,$extensionfilter='')
{
if(!is_array($extensionfilter)) {
$extensionfilter = array($extensionfilter);
}
$folder = self::ensure_trailing_slash($folder);
if(!is_dir($folder)) {
return array();
}
$out = array();
if ($dh = @opendir($folder)) {
while (($file = readdir($dh)) !== false)
{
if(self::is_dot_file($file)) continue;
if(is_dir($folder.$file)) {
$out = array_merge($out,self::getAllFiles($folder.$file,$extensionfilter),array($folder.$file));
} elseif(!is_file($folder.$file)) {
continue;
} else {
if(empty($extensionfilter[0]) || preg_match('`\.'.implode('|',$extensionfilter).'`', $file)) {
$out[]=$folder.$file;
}
}
}
sort($out);
return $out;
}
}
public static function ensure_trailing_slash($folder)
{
$folder .= (substr($folder, -1) == '/' ? '' : '/');
return $folder;
}
public static function is_dot_file($file) {
return preg_match('`^\.`', $file);
}
/**
*
* Check if directory contains files
*
* @param $folder string Source folder
* @return boolean
*/
function hasFiles($folder)
{
if(!is_dir($folder)) {
return false;
}
if ($dh = @opendir($folder)) {
while (($file = readdir($dh)) !== false)
{
if(!is_file($folder.$file)) continue;
return true;
}
}
return false;
}
/**
*
* Get directory content
*
* @return Files list
*/
function getContents()
{
$out=array();
if(!is_dir($this->file)) {
return false;
}
if ($dh = @opendir($this->file))
{
while (($file = readdir($dh)) !== false)
{
if(!preg_match('`'.$this->_pattern.'`',$file)) {
$out[]=$file;
}
}
}
return $out;
}
/**
*
* Delete directory content
*
* @return Total of directory
*/
function deleteContents()
{
if ($dh = @opendir($this->file))
{
foreach($this->getContents() as $file) {
unlink($this->file.$file);
$nbd++;
}
}
return $nbd;
}
/**
*
* Output specified file
*
* @access public
* @param $file string File URL
*/
public function output($file,$asname='')
{
if(empty($asname)) {
$asname=basename($file);
}
switch(fileUtils::getFileExtension($file)){
case 'jpeg':
case 'jpg':
$ctype='image/jpeg';
break;
case 'gif':
$ctype='image/gif';
break;
case 'png':
$ctype='image/png';
break;
default:
$ctype='application/force-download';
header('content-disposition:attachment;filename='.$asname);
break;
}
header('Content-Type:'.$ctype);
readfile($file);
exit(0);
}
/**
*
* description
*
* @param $path
* @return unknown_type
*/
public static function delete($path)
{
if(is_file($path)) {
unlink($path);
} else {
exec('rm -Rf '.$path);
}
}
/**
* sanitizes a file name to prevent from evil behaviour
* and removes any path to keep only the file name
* @param $filename string file name to sanitize
* @return string sanitized file name
*/
public static function sanitize($filename)
{
$result = basename($filename);
return escapeshellcmd($result);
}
public function getHumanFileSize($file)
{
$sizeArr = array('b','Kb','Mb','Gb','Tb');
$size = filesize($file);
$humansize = $size;
$cycle=0;
while($humansize>1) {
$humansize/=1024;
$cycle++;
}
return round($humansize*1024,1).' '.__($sizeArr[$cycle-1]);
}
}