-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles_utils.php
84 lines (71 loc) · 2.01 KB
/
files_utils.php
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
<?php
// ==================================================
// * FilesUtils
class FilesUtils {
/*
* Returns an array that contains only the files of a directory
*/
public static function getFiles($path) {
return array_diff(scandir($path), array('.', '..'));
}
/*
* Removes the extension from a filename. "file.php" => "file"
*/
public static function removeExtension($filename) {
return preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
}
/*
* Returns the extension of a given file.
*/
public static function getExtension($filename) {
return preg_replace('/.+\./', '', $filename);
}
/*
* Delete a directory with recursion (including the files and another directories)
* Source: http://stackoverflow.com/a/3349792
*/
public static function deleteDir($dirPath) {
if (!is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
self::deleteDir($file);
} else {
unlink($file);
}
}
rmdir($dirPath);
}
/*
* List all the files and folders from a given directory
* Base from: http://stackoverflow.com/a/32078936
*/
public static function getAllFiles($dir, $basedir = '') {
$results = array();
$subresults = array();
if ($basedir == '') {
$basedir = realpath($dir).DIRECTORY_SEPARATOR;
}
$files = scandir($dir);
foreach ($files as $key => $value){
if ($value != '.' && $value != '..') {
$path = realpath($dir.DIRECTORY_SEPARATOR.$value);
$subresults[] = str_replace($basedir,'',$path);
if (is_dir($path)) {
$subdirresults = self::getAllFiles($path, $basedir);
$results = array_merge($results,$subdirresults);
}
}
}
if (count($subresults) > 0) {
$results = array_merge($subresults, $results);
}
return $results;
}
}
?>