0

The following code is meant to display directories as well as sub directories within the directories and the files inside them. I used chmod function to gain access to directories.

This code runs but doesn't display the directory hierarchy, which means it is unable to list the sub directories and the files inside.

As I run the script, I get this warning :

Warning: chmod(): No such file or directory in E:\Installed_Apps\xampp\htdocs\dlist.php on line 5

-

#recursive function

function directory_f_lister($root) {
    $dir_list = scandir($root);
    for($var=0;$var<count($dir_list);$var++) {

        $bool = chmod($root.$dir_list[$var], 0777);

        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/>";
                    }
                }
            }
        } else { "<b>else statement <br /> </b>"; }
    }
}

directory_f_lister(DIRECTORY_SEPARATOR);

What is the problem ? Why am I not getting the directory hierarchy ?

saplingPro
  • 20,769
  • 53
  • 137
  • 195

1 Answers1

0
chmod(realpath(dirname(__FILE__)).'/'.$dir_list[$var], 0777);
Deepanshu Goyal
  • 2,738
  • 3
  • 34
  • 61
  • print your realpath(dirname(__FILE__)) and then concatenate the filename and folder path as necessary – Deepanshu Goyal Oct 25 '13 at 06:05
  • that doesn't help ! Why would I use `dirname(__FILE__)` ? I want to print the directories starting from root `DIRECTORY_SEPARATOR` – saplingPro Oct 28 '13 at 05:38
  • Also, the statement `echo " $root.$dir_list[$var] "` prints like `\$AVG...$VAULT ` or `\.$AVG ` – saplingPro Oct 28 '13 at 05:39
  • @saplingPro is that what you expect as output ? chmod expects real path instead of hardcoded path sometimes... did you try : **realpath(dirname(__FILE__)).'/'.$root.'/'$dir_list[$var];** and echoed it – Deepanshu Goyal Oct 28 '13 at 05:55
  • yeah. but that gives the path from where the script is running – saplingPro Oct 28 '13 at 06:10
  • NO. I want to read all the directories and sub directories starting from the root. If its `E:\b\c\d\script.php`, start reading everything from `E:\\` – saplingPro Oct 28 '13 at 07:20