-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirListing.php
149 lines (120 loc) · 3.67 KB
/
DirListing.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
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
<?php
require('FileInfo.php');
class DirListing
{
private $showHiddenFiles = false;
private $showFolderSize = false;
private $showLastModified = false;
private $root;
private $current;
private $folderList;
private $fileList;
public function __construct($root)
{
$this->root = rtrim($root, '/');
$this->_current = trim(urldecode($_SERVER['REQUEST_URI']), '/') == '' ? '/' : '/' . trim(urldecode($_SERVER['REQUEST_URI']), '/') . '/';
}
public function getBreadCrumb()
{
$folders = explode('/', trim($this->getCurrentPath(), '/'));
$fathers = count($folders);
$breadCrumb = array(
array(
'link' => '/',
'label' => '..',
'active' => $this->getCurrentPath() == '/',
)
);
for ($i = 0 ; $i < $fathers ; $i++) {
if ($folders[$i] == '')
continue;
$link = '/';
for ($j = 0 ; $j <= $i ; $j++)
$link .= $folders[$j] . '/';
$breadCrumb[] = array(
'link' => $link,
'label' => $folders[$i],
'active' => $link == $this->getCurrentPath(),
);
}
return $breadCrumb;
}
public function scanDirectoryContents()
{
try {
$dirIterator = new DirectoryIterator($this->getFullPath());
} catch(UnexpectedValueException $e) {
return false;
}
$this->folderList = array();
$this->fileList = array();
foreach ($dirIterator as $fileInfo) {
if ((strpos($fileInfo->getFilename(), '.') === 0 && $this->showHiddenFiles === false) || $fileInfo->isDot())
continue;
elseif ($fileInfo->isDir())
$this->folderList[$fileInfo->getFilename()] = new FileInfo($fileInfo, $this->showFolderSize);
else
$this->fileList[$fileInfo->getFilename()] = new FileInfo($fileInfo, true);
}
ksort($this->folderList);
ksort($this->fileList);
return true;
}
public function isDownloadingFile()
{
return is_file(rtrim($this->getFullPath(), '/'));
}
public function downloadFile()
{
if (!$this->isDownloadingFile())
return;
$fullPath = rtrim($this->getFullPath(), '/');
$fileInfo = new SplFileInfo($fullPath);
header("Content-disposition: inline; filename=" . $fileInfo->getFileName());
header("Content-type: ". mime_content_type($fullPath));
readfile($fullPath);
}
public function isDirectoryEmpty()
{
return empty($this->folderList) && empty($this->fileList);
}
public function getDirectoryFolders()
{
return $this->folderList;
}
public function getDirectoryFiles()
{
return $this->fileList;
}
public function getCurrentPath()
{
return $this->_current;
}
public function getFullPath()
{
return $this->root . $this->getCurrentPath();
}
public function setShowHiddenFiles($showHiddenFiles = true)
{
$this->showHiddenFiles = $showHiddenFiles;
return $this;
}
public function setShowFolderSize($showFolderSize = true)
{
$this->showFolderSize = $showFolderSize;
return $this;
}
public function showFolderSize()
{
return $this->showFolderSize;
}
public function setShowLastModified($showLastModified = true)
{
$this->showLastModified = $showLastModified;
return $this;
}
public function showLastModified()
{
return $this->showLastModified;
}
}