请问,php怎样得到FTP上某个目录下的文件列表,并判断是否有包含"abc"的文件名?谢谢!

解决方案 »

  1.   


    $conn_id = ftp_connect($ftp_server);// login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);// get contents of the current directory
    $contents = ftp_nlist($conn_id, ".");
    $strContent = implode("****", $contents);
    if(preg_match("/abc/i", $strContent))
    {
      echo "true";
    }
    else
    {
    echo false;
    }
      

  2.   

    这个问题好多公司笔试的时候经常出这样的问题<?php
    $dir = 'dirname';
    function view_dir($directory)
    {
        $handle = opendir( $directory );
        while ( $file = readdir($handle) )
        {
            $bdir = $directory . '/' .$file;
            if ($file <> '.' && $file <> '..' && is_dir($bdir))
            {
                view_dir( $directory .'/'. $file);
            }
            else if( $file <> '.' && $file <> '..')
            {
                $return[] = $directory .'/'. $file;
                if($file=='abc'){
                   $exist = 1;
                }else{
                   $exist = 0;
                }
            }
        }
        closedir( $handle );
        $return[] = $exist;
        return $return[];
    }
    view_dir($dir);
    ?>
      

  3.   

    我这样写,他说
    Warning: ereg() [function.ereg]: REG_EMPTY in...on line 4$it="abc";
    $dir0=opendir('kk');
    while($file=readdir($dir0)){
        if (ereg($it,$file))    //4
    $a=$file;
    }我慢慢消化楼上朋友们说的,请纠正我的错误,谢谢!
      

  4.   

    1 你的是ftp列表,如何直接用opendir?
    2 ereg函数,第一个参数是$pattern,第二个参数是查询的字符串.
    pattern要用正则表达式的方式.
      

  5.   

    谢谢jakey9826!
    我这样写:
     if (ereg($it,$file))   //4 
        unlink("kk/$file");  
    他痛痛快快地把所有含"abc"的文件名都删了,
    可这样写就不对:
     if (ereg($it,$file))    //4 
        $a=$file; 
    “pattern要用正则表达式的方式”,这个应该怎样写呢?谢谢!
      

  6.   

    另外,怎样在所有含"abc"的文件中找到最后传上去的那个文件?谢谢!
      

  7.   

    多看看手册吧,
    如果只是搜索abc没有必要用正则.最后上传的文件,可以通过查看文件的最后修改日期.$file = 'somefile.txt';// set up basic connection
    $conn_id = ftp_connect($ftp_server);// login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);//  get the last modified time
    $buff = ftp_mdtm($conn_id, $file);if ($buff != -1) {
        // somefile.txt was last modified on: March 26 2003 14:16:41.
        echo "$file was last modified on : " . date("F d Y H:i:s.", $buff);
    } else {
        echo "Couldn't get mdtime";
    }// close the connection
    ftp_close($conn_id);