【问题标题】:php ftp_nlist function returns 504 Gateway Timeout errorphp ftp_nlist 函数返回 504 网关超时错误
【发布时间】:2020-05-11 15:00:25
【问题描述】:

我的 ftp_nlist 函数有问题,即当我在生产服务器上运行代码时它返回 504 网关超时错误,在 localhost 上运行正常。

我的代码是:

   $connection_id = ftp_connect( $host, $port, $timeout );

  if ( $connection_id === false ) {
    throw new \Exception( 'Unable to connect to FTP Server. Check the connection details and try again.' );
  }

  $login = @ftp_login($connection_id,  $username,  $password);

  if ( $login !== true ) {
    $error = 'Unable to log in. Check your username or password and try again.';
    throw new \Exception( $error );
  }

  $check_dir = @ftp_chdir( $connection_id, $ftp_folder );

  if ( !$check_dir ) {
    $error = 'Couldn\'t find directory '. esc_attr( $ftp_folder ).' on '. esc_attr( $host );
    throw new \Exception( $error );
  }

  $check_file = @ftp_nlist( $connection_id, $ftp_folder ) ?: array();

  // This is where error occurs

我尝试设置以下内容:

 ftp_set_option($connection_id, FTP_USEPASVADDRESS, false); // set ftp option
  ftp_pasv($connection_id, true); //make connection to passive mode

在 ftp_login 函数之后也不起作用。

知道这可能是什么问题吗?

提前致谢。

【问题讨论】:

    标签: php


    【解决方案1】:

    如果您正在登录和更改目录,我假设最初连接到服务器不是问题。

    删除ftp_nlist() 前面的@ 可能会为您提供有关错误情况性质的更多信息。

    顺便说一句,我唯一的想法是目录列表太长,以至于您的连接在服务器返回之前就超时了。您的脚本需要多长时间才能达到此错误?

    如果您使用ftp_rawlist()ftp_mlsd(),您会得到相同的行为吗?

    【讨论】:

    • 删除 @ 没有帮助。 ftp_rawlist() 也超时,ftp_mlsd() 返回 false。目录下只有2个文件
    • ftp_nlist() 调用之后的代码是什么?我知道您提到看到对 504 的引用,但您能提供完整的错误消息/堆栈跟踪吗?
    • 我得到一个空白页: 504 Gateway Time-out 服务器没有及时响应。 ftp_nlist() 之后的代码永远不会运行
    • 也许this thread 可能有帮助?
    • 它不起作用。我用 ftp_nlist() 删除了代码,现在 ftp_get() 遇到了同样的问题。它可以在我的本地站点上运行。会不会是服务器配置问题?
    【解决方案2】:

    不知道问题现在是否已解决,但我遇到了同样的问题。我认为这是大多数时间都被激活的防火墙问题。要修复它,我只需要添加这一行:

    ftp_pasv($ftp_conn,true);
    

    希望对某人有所帮助!

    【讨论】:

    • 请添加官方文档的链接,并详细说明此设置的所有后果。另见How To Answer Well
    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2011-04-08
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    相关资源
    最近更新 更多