-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_scan.php
executable file
·86 lines (59 loc) · 1.47 KB
/
file_scan.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
<?php
session_start();
register_shutdown_function(function(){
if(connection_status()==2){
echo 'There was a timeout unable to connect';
}
});
set_time_limit(5);
@ini_set("default_socket_timeout", 5);
include('db_connect.php');
include('checksession.php');
require_once('logging.php');
include('ssh.php');
$username = $_SESSION['username'];
mountUserFiles($username);
$dir = $username;
// Run the recursive function
$response = scan($dir);
// This function scans the files folder recursively, and builds a large array
function scan($dir){
$files = array();
// Is there actually such a folder/file?
$name_dir = $dir;
$dir = "files/".$dir;
if(file_exists($dir)){
foreach(scandir($dir) as $f) {
if(!$f || $f[0] == '.') {
continue; // Ignore hidden files
}
if(is_dir($dir . '/' . $f)) {
// The path is a folder
$files[] = array(
"name" => $f,
"type" => "folder",
"path" => $name_dir . '/' . $f,
"items" => scan($name_dir . '/' . $f) // Recursively get the contents of the folder
);
}
else {
// It is a file
$files[] = array(
"name" => $f,
"type" => "file",
"path" => $name_dir . '/' . $f,
"size" => filesize($dir . '/' . $f) // Gets the size of this file
);
}
}
}
return $files;
}
// Output the directory listing as JSON
header('Content-type: application/json');
echo json_encode(array(
"name" => $dir,
"type" => "folder",
"path" => $dir,
"items" => $response
));