Heyas--
I've been trying to code a FTP spider, to function as a search engine for the FTP servers on our network.
The code writes down all of the files with the ftp server name in front in text files.
It's about to work, just one little problem...
When I want to spider a directory with directories inside of it I have a small problem. The spider only writes down the first dir in the list. However, when there are only files inside of a directory that I want to spider the script writes them down perfectly...
A week or two ago I had gotten this almost working, however because of the end of school and such I had to postpone the coding, now I'm kinda lost in my own code...
<?php
$ftp = $_GET["ftp"];
$dir = $_GET["dir"];
$ftp_host = $ftp;
$ftp_user = "anonymous";
$ftp_password = "anonymous";
echo "Connecting to $ftp_host via FTP...<BR>";
flush();
ob_flush();
$conn = ftp_connect($ftp_host);
$login = ftp_login($conn, $ftp_user, $ftp_password);
$mode = ftp_pasv($conn, TRUE);
if ((!$conn) || (!$login) || (!$mode)) {
die("FTP connection has failed !");
}
echo "Login Ok.<BR>";
flush();
ob_flush();
$mode = ftp_pasv($conn, TRUE);
function itemize_dir($contents) {
foreach ($contents as $file) {
if(ereg("([-dl][rwxstST-]+).* ([0-9]*) ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9])[ ]+(([0-9]{2}:[0-9]{2})|[0-9]{4}) (.+)", $file, $regs)) {
$type = (int) strpos("-dl", $regs[1]{0});
$tmp_array['line'] = $regs[0];
$tmp_array['type'] = $type;
$tmp_array['rights'] = $regs[1];
$tmp_array['number'] = $regs[2];
$tmp_array['user'] = $regs[3];
$tmp_array['group'] = $regs[4];
$tmp_array['size'] = $regs[5];
$tmp_array['date'] = date("m-d",strtotime($regs[6]));
$tmp_array['time'] = $regs[7];
$tmp_array['name'] = $regs[9];
}
$dir_list[] = $tmp_array;
}
//return $tmp_array;
return $dir_list;
}
function spider($ftp, $dir, $conn) {
//$conn = $ftp_host;
$buff = ftp_rawlist($conn, $dir);
$items = itemize_dir($buff);
foreach($items as $line=>$item)
{
$file = $ftp . $dir . $item['name'];
$fh = fopen('ftp.txt', 'a') or die("can't open file");
fwrite($fh, $file . "\n");
fclose($fh);
echo $file;
echo "<BR>";
//echo $item['name'];
if(strpos("/", $ftp, $dir . $item['name']) == TRUE) {
$ydir = "/";
}
if($item['type'] == 1) {
return $dir . $item['name'] . "/";
} } }
$work = spider($ftp, '/', $conn );
while($var = 0) {
if( $work != 0 ){
$works = spider($ftp, $work, $conn );
}
else {
$var = 1;}
}
?>
And I do specify the ftp server in the variable: ?ftp=