PHP FTP ftp_nlist 不起作用,返回布尔值 false

2022-01-09 00:00:00 ftp php

我对这段代码有点问题.脚本连接,但它不会给我位于根目录的文件夹......我错过了什么?

I have a little problem with this pice of code. The script connects, but it wont give me the folders that are located in root ... i am missing something?

    $ftp_server = "ftp.something.com";
    $ftp_user = "user";
    $ftp_pass = "pass";

    // set up a connection or die
    $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

    // try to login
    if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) 
    {
        echo "Connected as $ftp_user@$ftp_server
";
    } 
    else 
    {
        echo "Couldn't connect as $ftp_user@$ftp_server
";
    }

    $contents = ftp_nlist($conn_id, ".");
    var_dump($contents);

    ftp_close($conn_id);  

    die;

输出

Connected as $ftp_user@$ftp_server;

boolean false

为什么不列出文件?

我可以用

  file_exists("ftp//user:pass@host.com") 

...但是简单的部分不是我想要的,我什么都学不会

... but the easy part is not what im looking for, i would not learn anything

推荐答案

ftp_nlist() 发生错误时返回false.我猜你需要使用被动传输:

ftp_nlist() returns false when an error occurs. I'm guessing you need to use passive transfer:

// after ftp_login(...)
ftp_pasv($conn_id, true);

Generell,我建议使用像 ftp 这样的 CLI 工具或像 Filezilla 这样的 GUI 客户端来解决这个问题.日志/输出非常非常有用.

Generell, I'd recommend troubleshooting this by using a a CLI tool like ftp or a GUI-client like Filezilla. The log/output is very, very helpful.

HTH

相关文章