Skip to content

Instantly share code, notes, and snippets.

@xxiz
Created December 25, 2022 01:37
Show Gist options
  • Save xxiz/b14986fdb3366696ce7b33b081127590 to your computer and use it in GitHub Desktop.
Save xxiz/b14986fdb3366696ce7b33b081127590 to your computer and use it in GitHub Desktop.
get information about directories
<?php
$cache_expiration = 3600; // 1 hour
$cache_file = '/path/to/cache/file.json';
if (file_exists($cache_file) && (time() - $cache_expiration < filemtime($cache_file))) {
header('Content-Type: application/json');
readfile($cache_file);
exit;
}
header('Content-Type: application/json');
$directories = array(
"safe" => "/home/safe/data",
"server" => "/home/sss/uploads"
);
$results = array();
foreach ($directories as $label => $dir) {
$file_count = 0;
$size = 0;
$handle = opendir($dir);
if ($handle) {
while (false !== ($entry = readdir($handle))) {
if (is_file($dir . '/' . $entry)) {
$file_count++;
$size += filesize($dir . '/' . $entry);
}
}
}
closedir($handle);
$size_mb = round($size / (1024 * 1024), 2);
$results[$label] = array(
"file_count" => $file_count,
"size_mb" => $size_mb,
"size" => $size
);
}
file_put_contents($cache_file, json_encode($results));
echo json_encode($results);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment