-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistFiles.php
More file actions
64 lines (52 loc) · 2.09 KB
/
Copy pathlistFiles.php
File metadata and controls
64 lines (52 loc) · 2.09 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
<?php
// Variables
$DirectoryToScan = "files/";
$FilesCount = 0;
$UsedSpaceInBytes = 0;
$CapacityInBytes = 200000000000;
$Files = array();
$FileTypes = array();
function file_extension($filename)
{
$path_info = pathinfo($filename);
return (isset($path_info['extension']) ? $path_info['extension'] : "");
}
function array_most_common($input)
{
$counted = array_count_values($input);
arsort($counted);
return(key($counted));
}
// Returns formated string with size of file
function format_size($size) {
$sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
if ($size == 0) { return('n/a'); } else {
return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }
}
// Scan specified folder
$DirectoryContent = scandir($DirectoryToScan);
natcasesort($DirectoryContent);
$FilesCount = count($DirectoryContent) -3; // -3 is because the array contains . and .. and .htaccess
// Open a known directory, and proceed to read its contents
foreach($DirectoryContent as $File)
{
if ($File != "." && $File != ".." && $File != ".htaccess") {
$FileType = strtolower(file_extension($File));
$FileSize = filesize($DirectoryToScan.$File); // Get file size in bytes
$FileTime=date('Y-m-d H:i:s', filemtime($DirectoryToScan.$File));
$Icon = "images/types/_blank.png";
if(file_exists("images/types/{$FileType}.png")) {
$Icon="images/types/{$FileType}.png";
}
// Add file size to the all used space value
$UsedSpaceInBytes = $UsedSpaceInBytes + $FileSize;
array_push($FileTypes, $FileType);
array_push($Files, array('filename' => $File, 'icon' => $Icon, 'filetype' => $FileType, 'filesize' => format_size($FileSize), 'filecreated' => $FileTime));
}
}
$stats[] = array('storagecapacity' => format_size($CapacityInBytes), 'usedcapacity' => format_size($UsedSpaceInBytes), 'totalfiles' => $FilesCount, 'populartype' => array_most_common($FileTypes));
$output = array();
array_push($output, array('files' => $Files, 'statistics' => $stats));
header('Content-Type: application/json');
echo(json_encode($output));
?>