| 
<?
 /**
 * ADV_FTP a simple PHP spider class to indexing remote recursive directory and handy to put MySQL .
 * Support now UNIX and Windows_NT FTP filesystem.
 * 2011-07-31 Tomasz Malewski - varsovie (nospam) o2.pl
 */
 class adv_ftp {
 
 function adv_ftp () {
 $this->level = 1;
 $this->pwd = "";
 }    // adv_ftp init
 
 // Estabilish connection
 public function conn($hostname, $username, $password) {
 $this->hostname=$hostname;
 $this->username=$username;
 $this->password=$password;
 $this->conn_id = ftp_connect($this->hostname);
 $login_result = ftp_login($this->conn_id, $this->username, $this->password);
 ftp_pasv($this->conn_id, true);
 $this->systype = ftp_systype($this->conn_id);        // Estimate FTP System type to analyze date
 //        if ((!$conn_id) || (!$login_result)) { echo " *** ADV_FTP : Unable to connect <br>\r\n";    }
 }    // conn end
 
 // Classid dir for FTP , return array $this->dir
 public function listFiles() {
 //        $buff = ftp_rawlist($this->conn_id, '.', true);        // recursive dir  3rd parameter PHP 4.3
 $buff = ftp_rawlist($this->conn_id, '.');
 //        var_dump($buff);
 $this->pwd = ftp_pwd($this->conn_id);
 foreach ($buff as $line) {
 switch ($this->systype) {        // select UNIX or Windows NT FTP remote
 
 case "UNIX":
 $finfo = preg_split("/[\s]+/", $line, 9);
 if ($finfo[0] !== "total") {
 //                    print_R ($finfo);
 $filepath = $this->hostname."".$this->pwd."/".$finfo[8];
 $this->dir[$filepath][filename] = $finfo[8];
 $this->dir[$filepath][dir] = $this->pwd;
 $this->dir[$filepath][date] = strtotime("$finfo[5] $finfo[6] $finfo[7]");
 $this->dir[$filepath][size] = $finfo[4];
 if (preg_match("/^d/",$finfo[0])) { $this->dir[$filepath][isdir]='1';}
 }    // if vinfo != 0
 break;
 
 case "Windows_NT":
 ereg("([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)",$line,$finfo);
 $filepath = $this->hostname."".$this->pwd."/".$finfo[8];
 $this->dir[$filepath][filename] = $finfo[8];
 $this->dir[$filepath][dir] = $this->pwd;
 $this->dir[$filepath][isdir] = ($finfo[7]=="<DIR>");
 $this->dir[$filepath][date] = strtotime($finfo[1]."/".$finfo[2]."/".$finfo[3]);
 $this->dir[$filepath][size] = $finfo[7];
 //                print_r ($finfo);
 
 break;
 
 }    // switch systype
 }    // line
 }    // listfiles end
 
 // recursive dir. changeDir should be execute before to set start point. Don't forget to set $this->level
 function recursive($directory,$level)    {
 ob_end_flush(); ob_flush(); flush(); ob_start();
 if ($directory == FALSE ) $directory = $this->pwd;
 if ($level <= $this->level)
 {
 $this->changeDir("".$directory);
 $this->listfiles();
 //             print_R ($this->dir);
 foreach ($this->dir as $filename=>$key) {
 if ($key[isdir]==1) {$newlevel=$level+1;$this->recursive($key[dir]."/".$key[filename]."",$newlevel);}
 
 }        // each dir
 }    // white level
 }    // recursive end
 
 public function changeDir($dir) {
 ftp_chdir($this->conn_id, "/".$dir);
 $this->pwd=$dir;
 }    // changeDir
 
 public function close() {
 ftp_close($conn_id);
 }    // close end
 
 
 }    // adv_ftp end
 |