【问题标题】:PHP ftp_nlist() - timeoutPHP ftp_nlist() - 超时
【发布时间】:2014-02-20 02:09:04
【问题描述】:

我有一个奇怪的问题:我可以连接到 ftp 服务器,创建目录但我无法获取目录中的文件列表。

可能出了什么问题?

事实:

  • 服务器位于防火墙后面
  • 是的,我使用被动模式,从代码中可以看出
  • PHP 脚本位于 FTP 服务器所在的同一台服务器上
  • 我可以使用 FTP 客户端从我的桌面访问 FTP,一切正常,我可以创建/上传和读取目录内容。
  • ftp_pwd()ftp_mkdir() 工作快,ftp_nlist()ftp_rawlist() 等待在 ftp_connect() 中指定的超时(示例中为 10 秒)
  • 在活动模式下 - 结果几乎相同(red-dir-functions 立即失败)

我认为问题不在脚本中,而在 ftp/server/firewall/access 权限设置中。

有我的代码示例:

$conn = ftp_connect('my.host.here', 21, 10);

if (!$conn) {
    throw new Exception('Unable to connect to FTP');
}

$login_result = ftp_login($conn, 'login', 'password');
if (!$login_result) {
    throw new Exception('Unable to login to FTP');
}

// This is important part, because my server is behind frirewall/nat.
$paswRes = ftp_pasv($conn, true);
if (!$paswRes) {
    throw new Exception('Failed to enable passive FTP mode');
}

$resPwd = ftp_pwd($conn); //Returns: "/"

$resMkdir = ftp_mkdir($conn , 'testDir'); //Returns: "/testDir", creates new dir

$resNlist = ftp_nlist($conn, '.'); //Returns: bool(false)

$resRawlist = ftp_rawlist($conn, '.'); //Returns: bool(false)

更新:

如果主机是localhost,它可以工作。所以看起来问题出在防火墙/ftp服务器设置中。 我在Amazon EC2上使用ProFtpd

指定为PassivePorts 49152 65534的被动端口

此范围已作为入站端口范围添加到 Amazon EC2 安全组49152 - 65534 0.0.0.0/0

【问题讨论】:

    标签: php ftp


    【解决方案1】:

    好的,我已经解决了这个问题。 这是 FTP 服务器配置错误(在我的情况下 - ProFTPD 服务器): 除了PassivePorts 我必须指定MasqueradeAddress

    MasqueradeAddress                <your-Ftp-Server-Domain-Name-Or-Ip-Address>
    

    更新:另外,请查看这篇文章:http://www.elitehosts.com/blog/php-ftp-passive-ftp-server-behind-nat-nightmare/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多