ftp_get 在实时服务器上失败返回 false但在本地工作

问题描述

我正在尝试使用此脚本从 FTP 源下载 zip 文件(在 localhost 上它可以工作,但在 OVH 形式的实时服务器上不起作用)。

在实时服务器上运行时,我立即得到:

  • 成功连接到 ftp 服务器!
  • 登录成功!
  • 从...下载时出错。
  • 连接成功关闭

所以所有连接都很好,但在下载文件时卡住了。

可能是什么问题,或者我怎么能得到一些错误报告?

有些人认为我已经检查过了:

  • 已启用 FTP 支持(在 PHP 中)
  • 文件夹的权限为 777;
  • 端口已打开 (21);
  • PHP 最大执行时间很长 (300);
  • 主机提供的防火墙已禁用(我无法控制是否只是打开/关闭

还能是什么?

谢谢


// Connect to FTP server

// Use a correct ftp server
$ftp_server = "localhost";

// Use correct ftp username
$ftp_username="user";

// Use correct ftp password corresponding
// to the ftp username
$ftp_userpass="user";
    
// Establishing ftp connection
$ftp_connection = ftp_connect($ftp_server,21)
    or die("Could not connect to $ftp_server");

if( $ftp_connection ) {
    echo "successfully connected to the ftp server!";
    
    // Logging in to established connection
    // with ftp username password
    $login = ftp_login($ftp_connection,$ftp_username,$ftp_userpass);
    
    if($login) {
        
        // Checking whether logged in successfully
        // or not
        echo "<br>logged in successfully!";
        
        // Name or path of the localfile to
        // where the file to be downloaded
        $local_file = "file.zip";
        
        // Name or path of the server file to
        // be downoaded
        $server_file = "file.zip";
        
        // Downloading the specified server file
        if (ftp_get($ftp_connection,$local_file,$server_file,FTP_BINARY)) {
            echo "<br>Successfully downloaded from"
                . " $server_file to $local_file.";
        }
        else {
            echo "<br>Error while downloading from"
                . " $server_file to $local_file.";
        }
            
    }
    else {
        echo "<br>login Failed!";
    }
    
    // echo ftp_get_option($ftp_connection,1);
    // Closeing connection
    
    if(ftp_close($ftp_connection)) {
        echo "<br>Connection closed Successfully!";
    }
}
?>

解决方法

ftp_get 问题的最典型原因是 PHP 默认为 FTP 活动模式。并且在 99% 的情况下,必须切换到 FTP 被动模式才能进行传输。在 ftp_login 之后使用 ftp_pasv function

ftp_pasv($connect,true) or die("Passive mode failed");