LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 794|回复: 1

[php]ftp_rawlist()的疑惑

[复制链接]
发表于 2003-10-6 01:39:24 | 显示全部楼层 |阅读模式
定义为:
array ftp_rawlist ( resource ftp_stream, string directory)
对这其中的directory所使用的字符有什么限制?
我编了一下脚本,用来在指定ftp服务器上搜索文件.
[php]
<?php
/* To search a file on ftp server using a regular expression.
   To call:
           ftp_search.php regx ftp_server user password
        OR
        ftp_search.php regx ftp://user:password@ftp_server/dir
        OR
        ftp_search.php regx user:password@ftp_server/dir
*/
//Input process
$file = $argv[1];
$file_reg = "/" . $file . "/i";
if ($argv[2])
        list($user,$password,$ftp_site,$port,$dir) = url_parse($argv[2]);
if(!$ftp_site)
        $ftp_site = "127.0.0.1";
if(!$user)
        $user = "anonymous";
if(!password)
        $password = "somebody@earth.com";
if(!$dir)
        $dir = "/";
if(!$port)
        $port = 21;
if($argv[3])
        $user = $argv[3];
if($argv[4])
        $password = $argv[4];
//input test
/*        echo "user is $user\n";
        echo "password is $password\n";
        echo "ftp_site is $ftp_site\n";
        echo "port is $port\n";
        echo "dir is $dir\n";
*/
//input process ends

while(true)
{
$ftp = ftp_connect($ftp_site,$port);
if ($ftp)
{
        echo "Connected succeesfully!\n";
        echo "Remote system is " . ftp_systype($ftp) . "\n";
}
else
{
        echo "Connection failed!\n";
        continue;
}
$status = @ftp_login($ftp,$user,$password);
if ($status)       
{
        echo "Login successfully\n";
        break;
}
else
{
        echo "Login failed\n";
        echo "I'll try again\n";
        ftp_close($ftp);
}
}
if(ftp_pasv($ftp,true))
        echo "Enter pasv mode\n";
search($dir,$file_reg);
if($ftp)
        ftp_close($ftp);

function search($dir,$file_reg)
{
        global $file;
        global $ftp;
        if(!ftp_chdir($ftp,$dir))
        {
                echo "Failed to chang to $dir \n";
                return false;
        }else{
        //        echo "Successfully changed into $dir\n";
        //        echo "Searching in $dir\n";
        }
        if(! $item = ftp_rawlist($ftp,$dir))
                echo "Failed to list " . $dir . "\n";
        else
        {
                foreach($item as $key => $each)
                {
                        list($permission,$owner,$group,$filesize,$month,$mday,$time,$filename) = list_parse($each);
                        //echo "item[$key => $each]\n";
                        //$row = preg_split("/\s+/",$each);
                        //echo "Matching $file_reg, $row[8]\n";
                        if(preg_match("/^d/",$permission))
                                if ( $filename != "." && $filename != "..")
                                {
                                        $dirname = (ftp_pwd($ftp) == "/") ? "" : ftp_pwd($ftp);
                                        $dir = $dirname . "/$filename" ;
                                        //echo "$dir is a directory!";
                                        search($dir,$file_reg);
                                }
                        if(preg_match($file_reg,$filename))
                        {
                                $dirname = (ftp_pwd($ftp) == "/") ? " " : ftp_pwd($ftp);
                                echo "Find: " . $dirname . "/$filename\n";
                        }
                }
        }
        ftp_cdup($ftp);
        //echo "Now working in " . ftp_pwd($ftp) . "\n";
}

function url_parse($url)
{
//echo "url is $url\n";
preg_match("/^(ftp:\/\/)?(\w+:\w+@)?([^\/:]*)(:\d*)?(\/.*)?/i",$url, $matches);

$host = $matches[3];
$dir = $matches[5];
if($matches[2])
{
        preg_match("/(\w+)\w+)@/",$matches[2],$user_passwd);
        $user = $user_passwd[1];
        $password = $user_passwd[2];
/*
        echo "user is $user\n";
        echo "password is $password\n";
        echo "host is $host\n";
        echo "dir is $dir\n";
*/
}
if($matches[4])
{
        $port = ltrim($matches[4],":");
        echo "port is $port\n";
}
return array($user,$password,$host,$port,$dir);
}
function list_parse($list)
{
        //echo "list is:\n$list\n";
        preg_match("/(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)/",$list,$matches);
        //echo $matches[0];
        $permission = $matches[1];
        $owner = $matches[3];
        $group = $matches[4];
        $filesize = $matches[5];
        $month = $matches[6];
        $mday = $matches[7];
        $time = $matches[8];
        $filename = $matches[9];
        /*
        echo "permission is $permission\n";
        echo "owner is $owner\n";
        echo "group is $group\n";
        echo "filesize is $filesize\n";
        echo "month is $month\n";
        echo "mday is $mday\n";
        echo "time is $time\n";
        echo "filename is $filename\n";
        */
        return array($permission,$owner,$group,$filesize,$month,$mday,$time,$filename);
}

?>
[/php]

出现的问题是,有些时候会出错:
failed to list directory:……
而这个目录我用其他的ftp工具是可以打开的。
而且当我在自己的机器上面建相同的目录结构时却又不出错。不知道是哪个地方是问题了
 楼主| 发表于 2003-10-6 01:48:16 | 显示全部楼层

发现网页对正则表达式支持不好

上面这帖子中的
preg_match("/^(ftp://)?(w+:w+@)?([^/:]*)(:d*)?(/.*)?/i",$url, $matches);
其实应该是:
preg_match("/^(ftp:\/\/)?(\w+:\w+@)?([^\/:]*)(:\d*)?(\/.*)?/i",$url, $matches);

preg_match("/(S+)s+(S+)s+(S+)s+(S+)s+(S+)s+(S+)s+(S+)s+(S+)s+(S.*)/",$list,$matches);
应该是:
preg_match("/(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)/",$list,$matches);
论坛中都把'\\'去掉了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表