PHP FTP recursive directory listing
<p>I'm trying to make a recursive function to get all the directories and sub directories from my ftp server in an array.</p>

<p>I tried a lot of functions I've found on the web. The one that works best for me is this one: </p>

<pre><code>public function getAllSubDirFiles() {
    $dir = array(".");
    $a = count($dir);
    $i = 0;
    $depth = 20;
    $b = 0;
    while (($a != $b) &amp;&amp; ($i &lt; $depth)) {
        $i++;
        $a = count($dir);
        foreach ($dir as $d) {
            $ftp_dir = $d . "/";
            $newdir = ftp_nlist($this-&gt;connectionId, $ftp_dir);
            foreach ($newdir as $key =&gt; $x) {

                if ((strpos($x, ".")) || (strpos($x, ".") === 0)) {
                    unset($newdir[$key]);
                } elseif (!in_array($x, $dir)) {
                    $dir[] = $x;
                }
            }
        }
        $b = count($dir);
    }
    return $dir ;

}
</code></pre>

<p>The problem with this function is it wont allow the directory to have a "." in it's name and every file that is located in the root directory will be considered a directory as well. So I adjusted the function and got this: </p>

<pre><code>public function getAllSubDirFiles($ip, $id, $pw) {
    $dir = array(".");
    $a = count($dir);
    $i = 0;
    $depth = 20;
    $b =0;
    while (($a != $b) &amp;&amp; ($i &lt; $depth)) {
        $i++;
        $a = count($dir);
        foreach ($dir as $d) {
            $ftp_dir = $d . "/";
            $newdir = ftp_nlist($this-&gt;connectionId, $ftp_dir);
            foreach ($newdir as $key =&gt; $x) {
                if (!is_dir('ftp://'.$id.':'.$pw.'@'.$ip.'/'.$x)) {
                    unset($newdir[$key]);
                } elseif (!in_array($x, $dir)) {
                    $dir[] = $x;
                }
            }
        }
        $b = count($dir);
    }
    return $dir ;

}
</code></pre>

<p>This works pretty good but and gives the result I want. but it's so slow it's unusable. </p>

<p>I also tried working with <code>ftp_rawlist</code> but it has the same drawback of being horribly slow.</p>

<pre><code>public function getAllSubDirFiles() {
    $dir = array(".");
    $a = count($dir);
    $i = 0;
    $depth = 20;
    $b = 0;
    while (($a != $b) &amp;&amp; ($i &lt; $depth)) {
        $i++;
        $a = count($dir);
        foreach ($dir as $d) {
            $ftp_dir = $d . "/";
            $newdir = $this-&gt;getFtp_rawlist('/' . $ftp_dir);
            foreach ($newdir as $key =&gt; $x) {

                $firstChar = substr($newdir[$key][0], 0, 1);

                $a = 8;
                    while ($a &lt; count($newdir[$key])) {

                        if ($a == 8) {
                            $fileName = $ftp_dir . '/' . $newdir[$key][$a];
                        } else {
                            $fileName = $fileName . ' ' . $newdir[$key][$a];
                        }
                        $a++;
                    }

                if ($firstChar != 'd') {
                    unset($newdir[$key]);
                } elseif (!in_array($fileName, $dir)) {

                    $dir[] = $fileName;
                }
            }
        }
        $b = count($dir);
    }
    return $dir;
}

public function getFtp_rawlist($dir) {

    $newArr = array();

    $arr = ftp_rawlist($this-&gt;connectionId, $dir);

    foreach ($arr as $value) {

        $stringArr = explode(" ", $value);

        $newArr[] = array_values(array_filter($stringArr));
    }
    return $newArr;
}
</code></pre>

<p>I've been stuck on this problem for the last couple of days and I'am getting desperate. If any one has any suggestion please let me know</p>