web-learning-manager/scan.php

57 lines
1.2 KiB
PHP
Raw Normal View History

2021-01-19 16:53:20 +01:00
<?php
$dir = "Home";
// Run the recursive function
$response = scan($dir);
// This function scans the files folder recursively, and builds a large array
2022-07-03 22:27:47 +02:00
function scan($dir)
{
2021-01-19 16:53:20 +01:00
$files = array();
// Is there actually such a folder/file?
2022-07-03 22:27:47 +02:00
if (file_exists($dir)) {
foreach (scandir($dir) as $f) {
if (!$f || $f[0] == '.') {
2021-01-19 16:53:20 +01:00
continue; // Ignore hidden files
}
2022-07-03 22:27:47 +02:00
if (is_dir($dir . '/' . $f)) {
2021-01-19 16:53:20 +01:00
// Remove Synology Temp-Folders, if they exists!
2022-07-03 22:27:47 +02:00
if ($f != '@eaDir') {
2021-01-19 16:53:20 +01:00
// The path is a folder
$files[] = array(
"name" => $f,
"type" => "folder",
"path" => $dir . '/' . $f,
"items" => scan($dir . '/' . $f) // Recursively get the contents of the folder
);
}
2022-07-03 22:27:47 +02:00
} else if (is_file($dir . '/' . $f)) {
2021-01-19 16:53:20 +01:00
// It is a file
$files[] = array(
"name" => $f,
"type" => "file",
"path" => $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" => basename($dir),
"type" => "folder",
"path" => $dir,
"items" => $response
2022-07-03 22:27:47 +02:00
));