list directory class
This example will create an array of the contents of a directory and sort this array based on the filename in Descending order
It will then first simply dump the array using var_dump and then show how the array can be outputted in a pretty way by writing two custom functions for this purpose
<?php
$FD = new ListDir("testfiledir");
$arr = $FD->listContent("NAME DESC");
echo "<pre>\n";
var_dump($arr);
echo "</pre>\n";
// or:
$FD->showOutput($arr, "doDir", "doFiles");
function doDir($arr, $current, $total)
{
if ($current == 1)
{
echo "<ul>\n";
}
echo " <li>DIR: ".$arr['name']."</li>\n";
if ($current == $total && $arr['files'] == 0)
{
echo "</ul>\n";
}
}
function doFiles($arr, $current, $total, $totalDirs)
{
if ($totalDirs == 0 && $current == 1)
{
echo "<ul>\n";
}
echo " <li>".$arr['name']."</li>\n";
if ($current == $total)
{
echo "</ul>\n";
}
}
?>
Output
array(2) {
["dirs"]=>
array(2) {
[0]=>
array(6) {
["name"]=>
string(7) "subdir2"
["modified"]=>
int(1217483757)
["accessed"]=>
int(1217673542)
["size"]=>
int(0)
["files"]=>
int(4)
["list"]=>
array(2) {
["dirs"]=>
array(0) {
}
["files"]=>
array(4) {
[0]=>
array(5) {
["name"]=>
string(6) "vier.4"
["ext"]=>
string(1) "4"
["size"]=>
int(0)
["modified"]=>
int(1217483759)
["accessed"]=>
int(1217483757)
}
[1]=>
array(5) {
["name"]=>
string(6) "twee.2"
["ext"]=>
string(1) "2"
["size"]=>
int(0)
["modified"]=>
int(1216933542)
["accessed"]=>
int(1217395870)
}
[2]=>
array(5) {
["name"]=>
string(5) "een.1"
["ext"]=>
string(1) "1"
["size"]=>
int(0)
["modified"]=>
int(1216933537)
["accessed"]=>
int(1217395870)
}
[3]=>
array(5) {
["name"]=>
string(6) "drie.3"
["ext"]=>
string(1) "3"
["size"]=>
int(0)
["modified"]=>
int(1216933546)
["accessed"]=>
int(1217395870)
}
}
}
}
[1]=>
array(6) {
["name"]=>
string(6) "subdir"
["modified"]=>
int(1217395870)
["accessed"]=>
int(1217673542)
["size"]=>
int(9)
["files"]=>
int(2)
["list"]=>
array(2) {
["dirs"]=>
array(2) {
[0]=>
array(6) {
["name"]=>
string(10) "subberdir2"
["modified"]=>
int(1217395870)
["accessed"]=>
int(1217673542)
["size"]=>
int(0)
["files"]=>
int(2)
["list"]=>
array(2) {
["dirs"]=>
array(0) {
}
["files"]=>
array(2) {
[0]=>
array(5) {
["name"]=>
string(8) "sub.sub2"
["ext"]=>
string(4) "sub2"
["size"]=>
int(0)
["modified"]=>
int(1216933521)
["accessed"]=>
int(1217395870)
}
[1]=>
array(5) {
["name"]=>
string(8) "sub.sub1"
["ext"]=>
string(4) "sub1"
["size"]=>
int(0)
["modified"]=>
int(1216933515)
["accessed"]=>
int(1217395870)
}
}
}
}
[1]=>
array(6) {
["name"]=>
string(10) "subberdir1"
["modified"]=>
int(1217395870)
["accessed"]=>
int(1217673542)
["size"]=>
int(0)
["files"]=>
int(0)
["list"]=>
array(2) {
["dirs"]=>
array(0) {
}
["files"]=>
array(0) {
}
}
}
}
["files"]=>
array(2) {
[0]=>
array(5) {
["name"]=>
string(9) "blaat.aap"
["ext"]=>
string(3) "aap"
["size"]=>
int(9)
["modified"]=>
int(1216933528)
["accessed"]=>
int(1217395870)
}
[1]=>
array(5) {
["name"]=>
string(10) "aap.nootje"
["ext"]=>
string(6) "nootje"
["size"]=>
int(0)
["modified"]=>
int(1216791476)
["accessed"]=>
int(1217395870)
}
}
}
}
}
["files"]=>
array(5) {
[0]=>
array(5) {
["name"]=>
string(5) "3.txt"
["ext"]=>
string(3) "txt"
["size"]=>
int(0)
["modified"]=>
int(1216791435)
["accessed"]=>
int(1217395870)
}
[1]=>
array(5) {
["name"]=>
string(5) "2.txt"
["ext"]=>
string(3) "txt"
["size"]=>
int(0)
["modified"]=>
int(1216791439)
["accessed"]=>
int(1217395870)
}
[2]=>
array(5) {
["name"]=>
string(5) "1.zxt"
["ext"]=>
string(3) "zxt"
["size"]=>
int(0)
["modified"]=>
int(1216791458)
["accessed"]=>
int(1217395870)
}
[3]=>
array(5) {
["name"]=>
string(5) "1.txt"
["ext"]=>
string(3) "txt"
["size"]=>
int(0)
["modified"]=>
int(1216791430)
["accessed"]=>
int(1217395870)
}
[4]=>
array(5) {
["name"]=>
string(5) "1.axt"
["ext"]=>
string(3) "axt"
["size"]=>
int(0)
["modified"]=>
int(1216791451)
["accessed"]=>
int(1217395870)
}
}
}
- DIR: subdir2
- vier.4
- twee.2
- een.1
- drie.3
- DIR: subdir
- DIR: subberdir2
- DIR: subberdir1
- blaat.aap
- aap.nootje
3.txt
2.txt
1.zxt
1.txt
1.axt