I have to deploy this code that will list all the directories, sub directories and files in it starting from the root. The code works but I am not sure if this is the correct way to list. It should not fail, though.
Also, in despite not allowing directories with the name .
and ..
, they get printed. Why is that?
<?php
# Snippet that lists all the directories,sub directories and files under that directory
# recursive function
function directory_f_lister($root) {
$dir_list = scandir($root);
for($var=0;$var<count($dir_list);$var++) {
if(is_readable($root.$dir_list[$var])) {
if(is_dir($root.$dir_list[$var])) {
if($dir_list[$var] === "." || $dir_list[$var] === "..") continue;
echo "<h3>Name of directory $dir_list[$var]</h3>";
echo "<br />";
$dh = opendir($root.$dir_list[$var]);
while(($name = readdir($dh)) !== false) {
if(is_dir($root.$dir_list[$var].$name)) {
if($dir_list[$var] === "." || $dir_list[$var] === "..") continue;
echo "Name of directory : <strong> $name </strong>";
echo "<br />";
directory_f_lister($root.$dir_list[$var].$name);
}else {
echo $name;
echo "<br/>";
}
}
}
}
}
}
directory_f_lister(DIRECTORY_SEPARATOR);
#end