-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filebrowser.php
94 lines (77 loc) · 2.53 KB
/
Filebrowser.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
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Filebrowser
*
* This class represents a simple filebrowser implementation Based on the SPL DirectoryIterator class.
* It contains a constructor which takes 1 argument: the base Folder to initialise the file browser
* 2 methods:
* - getFolders: get all folders in the given path
* - getFiles: get all files in the given path
* @author Yannick De Jonghe <yannickdejonghe@gmail.com>
*
*/
class Filebrowser {
private $baseDir = "/";
private $currentPath = "/";
public function __construct($dir = "") {
$this->baseDir = $dir;
$this->currentPath = $this->baseDir;
}
/****************************************************************
Function returns all folders in the given subpath of the current path
If $namesOnly = true, it only returns the names,
If $namesOnly = false, it returns all SPL-FileInfoObjects
***************************************************************/
public function getFolders($path = "",$namesOnly = true) {
$this->setPath($path,false);
$di = new \DirectoryIterator ($this->currentPath);
$folders = array();
foreach ($di as $f) {
if(!$f->isDot() && $f->isDir()) {
if($namesOnly)
array_push($folders,$f->getFilename());
else
array_push($folders,$f);
}
}
return $folders;
}
/****************************************************************
Function return all files in the given subpath of the current path
If $namesOnly = true, it only returns the names,
If $namesOnly = false, it returns all SPL-FileInfoObjects
***************************************************************/
public function getFiles($path = "", $namesOnly = true) {
$this->setPath($path,false);
$di = new \DirectoryIterator ( $this->currentPath );
$files = array();
foreach ($di as $f) {
if(!$f->isDot() && $f->isFile()) {
if($namesOnly)
array_push($files,$f->getFilename());
else
array_push($files,$f);
}
}
return $files;
}
/****************************************************************
Function that adds $path to the current path if $overwrite = false.
if $overwrite = true, the current path will be reset to $path
***************************************************************/
public function setPath($path, $overwrite = false) {
if($overwrite) {
$this->currentPath = $path;
}
else {
while(substr($path, 0,1) == '/') {
$path = substr($path,1);
}
$this->currentPath .= $path;
if(substr($this->currentPath, -1) != '/') {
$this->currentPath .= "/";
}
}
}
}
?>