dongxun2089 2010-11-03 15:39
浏览 77
已采纳

通过FTP获取文件列表

I want to print the list of files and only files from an FTP server, here is what I could accomplish.

<?php
    $ftp_server = "my ftp server";
    $conn_id = ftp_connect($ftp_server);
    $ftp_user_name = "ftp username";
    $ftp_user_pass = "ftp password";
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    $contents = ftp_nlist($conn_id, '/');
    for ($i = 0 ; $i < count($contents) ; $i++)
        echo "<li>" . substr($contents[$i],1) . "</li>";
    ftp_close($conn_id);
?>

but this prints the names of files and folders. How can I just print the names of files (files may not have extensions!)

  • 写回答

5条回答 默认 最新

  • dongya9346 2010-11-03 15:52
    关注

    Options:

    1) you can use ftp_rawlist instead of ftp_nlist to get the full listing for the file/directory, which should indicate whether it's a directory. However, the format of that listing will depend on the operating system of the ftp server. For example, on a unix/linux system the raw listing might look something like this:

    drwxrwxr-x  3 jm72 jm72  4096 Nov  2 16:39 myDir
    -rw-rw-r--  1 jm72 jm72   257 Nov  2 16:39 myFile
    

    where the "d" in the first column will tell you it's a directory. Not sure what it would look like on a Windows server.

    2) for each file name you return, try to CD into it. If you can, it's a directory!

    if (ftp_chdir($conn_id, substr($contents[$i],1)) {
      //it's a directory, don't include it in your list
      ftp_cdup($conn_id) //don't forget to go back up to the directory you started in!
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?