You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP ftp_nlist函数在Linux服务器失效,请求技术协助排查

Troubleshooting ftp_nlist() Returning False on Linux Servers

First, let's reference your code for context:

$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
if (@ftp_login($ftp_conn, $ftp_username, $ftp_userpass)) {
    $path = "./";
    $path1="./test";
    $file = "test.txt";
    $file_list = ftp_nlist($ftp_conn,$path);
}
// close connection
ftp_close($ftp_conn);

Here are the most likely issues and actionable fixes to resolve the ftp_nlist() failure on your Linux server:

  • Enable Passive FTP Mode
    Linux servers typically have stricter firewall rules that block active FTP connections. Unlike Windows environments which often auto-negotiate passive mode, you need to explicitly enable it right after logging in. Add this line inside the ftp_login() success block:

    ftp_pasv($ftp_conn, true);
    

    This switches the client to passive mode, which is far more firewall-friendly for server-to-server FTP interactions.

  • Stop Suppressing Login Errors
    The @ before ftp_login() hides critical authentication errors that might be happening silently on Linux. Remove the @ to expose login failures, or add explicit error checking to get detailed feedback:

    if (ftp_login($ftp_conn, $ftp_username, $ftp_userpass)) {
        // Your existing directory/list logic here
    } else {
        die("FTP login failed: " . ftp_get_error($ftp_conn));
    }
    

    This will confirm whether the issue stems from authentication rather than the ftp_nlist() call itself.

  • Verify Remote Directory Permissions
    Double-check that your FTP user has read and list permissions for the ./ directory on the remote FTP server. Linux enforces strict file system permissions—even if the user can log in, they might lack access to list the contents of the target directory.

  • Check SELinux Restrictions
    If your Linux server uses SELinux (common on RHEL/CentOS-based systems), it might block the PHP process from making outgoing FTP connections. For testing, temporarily disable SELinux with:

    sudo setenforce 0
    

    If this fixes the issue, don't leave SELinux disabled permanently—instead, add a targeted rule to allow your web server process (e.g., httpd) to connect to FTP servers.

  • Validate Remote FTP Server Config
    If the remote server is Linux-based (e.g., running vsftpd), confirm:

    • The LIST command isn't disabled in the server configuration.
    • Chroot jail settings aren't restricting the user's access to the ./ directory.
    • Passive mode is enabled (look for pasv_enable=YES in vsftpd.conf).

内容的提问来源于stack exchange,提问作者Raj Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:34:42