Late to the show, but to build off of the accepted answer...
If you want to have all the files and directories in as array (that can be prettied-up nicely with JSON.stringify in javascript), you can modify the function to:
function listFolderFiles($dir) {
$arr = array();
$ffs = scandir($dir);
foreach($ffs as $ff) {
if($ff != '.' && $ff != '..') {
$arr[$ff] = array();
if(is_dir($dir.'/'.$ff)) {
$arr[$ff] = listFolderFiles($dir.'/'.$ff);
}
}
}
return $arr;
}
For the Newbies...
To use the aforementioned JSON.stringify
, your JS/jQuery would be something like:
var ajax = $.ajax({
method: 'POST',
data: {list_dirs: true}
}).done(function(msg) {
$('pre').html(
'FILE LAYOUT<br/>' +
JSON.stringify(JSON.parse(msg), null, 4)
);
});
^ This is assuming you have a <pre>
element in your HTML somewhere. Any flavour of AJAX will do, but I figure most people are using something similar to the jQuery above.
And the accompanying PHP:
if(isset($_POST['list_dirs'])) {
echo json_encode(listFolderFiles($rootPath));
exit();
}
where you already have listFolderFiles
from before.
In my case, I've set my $rootPath
to the root directory of the site...
$rootPath;
if(!isset($rootPath)) {
$rootPath = $_SERVER['DOCUMENT_ROOT'];
}
The end result is something like...
| some_file_1487.smthng []
| some_file_8752.smthng []
| CSS
| | some_file_3615.smthng []
| | some_file_8151.smthng []
| | some_file_7571.smthng []
| | some_file_5641.smthng []
| | some_file_7305.smthng []
| | some_file_9527.smthng []
|
| IMAGES
| | some_file_4515.smthng []
| | some_file_1335.smthng []
| | some_file_1819.smthng []
| | some_file_9188.smthng []
| | some_file_4760.smthng []
| | some_file_7347.smthng []
|
| JSScripts
| | some_file_6449.smthng []
| | some_file_7864.smthng []
| | some_file_3899.smthng []
| | google-code-prettify
| | | some_file_2090.smthng []
| | | some_file_5169.smthng []
| | | some_file_3426.smthng []
| | | some_file_8208.smthng []
| | | some_file_7581.smthng []
| | | some_file_4618.smthng []
| |
| | some_file_3883.smthng []
| | some_file_3713.smthng []
... and so on...
Note: Yours will not look exactly like this - I've modified JSON.stringify
to display tabs (vertical pipes), align all keyed values, remove quotes off of keys, and a couple other things. I will modify this answer with a link if I ever get to uploading it, or get enough interest.