【问题标题】:ftp_get fails (returns false) on live server but works locallyftp_get 在实时服务器上失败(返回 false)但在本地工作
【发布时间】:2021-06-25 08:05:33
【问题描述】:

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

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

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

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

可能是什么问题,或者我如何获得一些错误报告?

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

  • 已启用 FTP 支持(在 PHP 中)
  • 文件夹的权限为 777;
  • 端口已打开 (21);
  • php max execution is long (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!";
    }
}
?>

【问题讨论】:

    标签: php ftp ovh


    【解决方案1】:

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

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

    【讨论】:

    • 啊,就是这样,谢谢!我实际上也添加了这个,但没有死亡条件。 (并认为它没有帮助)但这是因为我将它放在 $ftp_connection 变量而不是 ftp_login 之后。
    猜你喜欢
    • 2020-12-02
    • 2019-06-08
    • 1970-01-01
    • 2015-04-05
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    相关资源
    最近更新 更多