Skip to content

Instantly share code, notes, and snippets.

@NachoToast
Created May 14, 2021 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NachoToast/a9c198d6eec9bfe44446ae62cf5ac2c8 to your computer and use it in GitHub Desktop.
Save NachoToast/a9c198d6eec9bfe44446ae62cf5ac2c8 to your computer and use it in GitHub Desktop.
Get array of files and multidimensional array of folders and files (1 directory deep) in a directory.
var files =
<?php
$files = array();
foreach (glob('*.html') as $filename) {
$files[] = pathinfo($filename)['filename'] . "." . pathinfo($filename)['extension'];
}
echo json_encode($files);
?>,
folders =
<?php
$folders = array();
foreach (glob('*', GLOB_ONLYDIR) as $directory) {
$p = pathinfo($directory)['filename'];
$items = 0;
foreach (glob("$p/*.html") as $filename) {
$folders[$p][$items] = pathinfo($filename)['filename'] . "." . pathinfo($filename)['extension'];
$items++;
}
}
echo json_encode($folders);
?>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment