遍历FTP下目录和文件生成数据库--可以做小规模的FTP搜索引擎_PHP

WBOY
Release: 2016-06-01 12:34:57
Original
1164 people have browsed it

P>
//ftp信息
$ftp_server="xxxxxxxxxxx";   //FTP服务器的地址
$username="xxx";                           //要查询目录
$userpassword="xxxxxx";               //相映的密码
//数据库信息
$localhost="localhost";                    //一般是localhost
$datauser="xxx";                            //数据库用户名
$datapassword="xxxxx";                 //数据库用户密码
$database="xxxxx";                        //数据库名称

 

//数据表建立信息 realfly是表名
// CREATE TABLE realfly (
//   name text NOT NULL,
//   id tinyint(4) unsigned NOT NULL auto_increment,
//   date date DEFAULT '0000-00-00' NOT NULL,
//   time time DEFAULT '00:00:00' NOT NULL,
//   content longtext NOT NULL,
//   KEY id (id)
// );

?>


###################################################
#程序:              FTP搜索引擎递归函数文件                    #
#文件:              class.php                                                #
#姓名:              网络骑士                                               #
#所用技术:           php&mysql                                           #
#版本:              v1.0                                                      #
#联系:              shakespareshmily@sina.com.cn               #
###################################################

function happy($ftp_stream,$directory,$link)

{

 if(is_dir($directory))

 {

  //$today=(date("Y")."-".date("m")."-".date("d"));

  //$content=$directory;

  //$time=date("H:i:s");

  //$query="INSERT INTO realfly VALUES (', ', '$today', '$time', '$content');";

  //mysql_query($query,$link);

  //$color="FF3399";

  //echo  "".$directory."
";

  $result=ftp_nlist($ftp_stream,$directory);

  for ($j=0;$j

 

                                       happy($ftp_stream,$result[$j],$link);

                                    }

 }

 else {

  $basename=basename($directory);
  $file=explode(".",$basename);

  $id=$i++;

  $today=(date("Y")."-".date("m")."-".date("d"));


  $content=$directory;

  //$content4=$directory;

  $time=date("H:i:s");

  $query="INSERT INTO realfly VALUES ('$file[0]', '$id', '$today', '$time', '$content');";

  mysql_query($query,$link);

 echo $directory."
";
 }

}

?>


  include ("./search_ini.php");
  include ("./class.php");
###################################################
#程序:              FTP搜索引擎主文件                               #
#文件:              search.php                                              #
#姓名:              网络骑士                                               #
#所用技术:           php&mysql                                            #
#版本:              v1.0                                                       #
#联系:              shakespareshmily@sina.com.cn                #
###################################################

$ftp_stream=ftp_connect($ftp_server,21);
$login=ftp_login($ftp_stream,"$username","$userpassword");
if(!$login||!$ftp_stream){
                         echo "cannot connect to $ftp_server";
                         echo "try again";
                         }
else
                         {
                         echo "connect to $ftp_server
CONGRATULATIONS!

";
                         }
                         $link=mysql_pconnect("$localhost","$datauser","$datapassword");
                         mysql_select_db("$database",$link);
$this=ftp_pwd($ftp_stream);
$this=@ftp_chdir($ftp_stream,"");

$result=ftp_nlist($ftp_stream,$this);
for ($i=0;$i{
if (is_dir($result[$i]))
 {
 happy($ftp_stream,$result[$i],$link);
 }
 else {echo $result[$i]."
";
  $basename=basename($result[$i]);
  $file=explode(".",$basename);
  $id=$i;
  $today=(date("Y")."-".date("m")."-".date("d"));
  $content="$result[$i]";
  $time=date("H:i:s");
  $query="INSERT INTO realfly VALUES ( '$file[0]', '$id', '$today', '$time', '$content');";
  mysql_query($query,$link);
  }
}
?>

以上是后台的搜索处理程序,下面是用户查询处理程序

###########################################

#软件名:     FTP搜索引擎显示页面                   #

#版本:       v1.0                                                #

#所用技术:   php&mysql                                   #

#作者:       网络骑士                                        #

#联系方式:   shakespareshmily@sina.com.cn      #

###########################################

include ("./search_ini.php");

//connect to database

$link=mysql_pconnect("$localhost","$datauser","$datapassword") or die ("无法连接服务器,请稍后再试!");

//select database

mysql_select_db("$database",$link);

//make query

$query="select name,content from realfly where name='$query1' ";
echo "您所查询的字段:        "; echo "$query1.
";
$i=0;
$result=mysql_query($query,$link);
if($result)
{while(list($name,$content)=mysql_fetch_row($result))
  { echo "您所能查询到的结果:     ";
    $content3="ftp://xxxxxxxx";                  //你的FTP站点名称
    $content2=stristr($directory,"/var/ftp");
    $content=$content3.$content2;
    echo "$content
";
    $i++;
  }   }

 if($i==0)
                    {   $color="FF3399";
   echo  "真是太神奇了,居然没有找到,呵呵~~,再试试!
";}
 else echo "恭喜,您要查找的东东找到了,赶快下载吧!.
";
    ?>

需要说明的几点:

1:产生背景:由于我们站长让我做一个类似北大天网那样的FTP搜索的东西,以方便校内想下载软件而又一时半会儿找不到相映目录的网友,所以它就产生了。当然它的搜索能力远不如天网强大,而且它也不是真正意义上的搜索引擎,但是却可以完成FTP目录下遍历的功能(如果你的目录不是太深-----它可以处理到18层目录)

2:以上程序必须放在你所要搜索的目录下面才可以用----原因是权限问题

3:存在的问题

 a:如果处理的文件目录太深(比如30层,虽然很少人这么做,但是它处理起来就会跳过太深的目录----原因是服务器关于命令运行时间的限制)-------偶已经想到了解决的方法(理论阶段,由于现在忙于应付CPA的考试,所以没有实施)------即模仿flashget等的多线程下载的方式。得到当前目录下的文件,文件夹同时生成数组对每个文件夹逐个进行搜索或者同时进行搜索

b:有时候会出现这种情况,即:父目录的名字和子目录的相同,这时候会跳过这个父目录-----这个问题容易解决但是会加大处理的任务,加之最近没有时间改进,所以一直放置着^-^

浅妄薄见,望与斟酌

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!