-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdir.php
73 lines (58 loc) · 1.45 KB
/
dir.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
<?php
class Dir {
/**
* List Files & Folders in a given Directory
* @param string $dir Hold the path to the directory
* @return array Folders first, then files.
*/
public function ls($dir) {
if(!is_dir($dir))
return "Directory not found";
$content = array_diff(scandir($dir), array('.', '..'));
$folders = array();
$files = array();
$result = array();
# Sort folder and files
foreach ($content as $item) {
if(is_dir($dir.'/'.$item))
$folders[] = $item."/";
else
$files[] = $item;
}
# Loop through the content, folder first
sort($folders);
if(!empty($folders)){
foreach ($folders as $folder) {
$result[] = $folder;
}
}
sort($files);
if(!empty($files)){
foreach ($files as $file) {
$result[] = $file;
}
}
return $result;
}
/**
* Removes a folder and its content
* @param string $dir The directory to remove
* @return No return value
*/
public function rrmdir($dir) {
if (!is_dir($dir))
throw new Exception("Directory not found");
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object == "." && $object == "..")
continue;
if (filetype($dir."/".$object) == "dir")
rrmdir($dir."/".$object);
else
unlink($dir."/".$object);
}
reset($objects);
rmdir($dir);
}
}
?>