简体   繁体   English

ftp_get()损坏的大文件下载

[英]ftp_get() corrupt large file download

I have to download a large file(approx~-9mb) in .gz format from the server using ftp. 我必须使用ftp从服务器下载.gz格式的大文件(大约〜-9mb)。 I have written a function which downloads the file completely and correctly but when i put this code online , the file is downloaded but the file gets corrupt. 我编写了一个函数,可以完全正确地下载文件,但是当我将此代码置于在线状态时,文件已下载但文件已损坏。 Here is my code: 这是我的代码:

function downloadFile($ftp_server, $username, $password, $server_file, $local_file)
{

// download server file
    $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
    $login = ftp_login($ftp_conn, $username, $password);
    ftp_pasv($ftp_conn, true);
    if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII)) {
        echo "Successfully written to $local_file.";
        //   exit;

    } else {
        echo "Error downloading $server_file.";
    }

}

Here is how i call it 这是我的称呼

ini_set('max_execution_time', 0);
$username = "username";
$password = "password";
$xmlFile = __DIR__ . "/monster.xml.gz";
$local_file = __DIR__ . "/monster.xml";
$ftp_server = "ftp.monster.com";
$server_file = "/US~Partner~Sample Feed.xml.gz";


downloadFile($ftp_server,$username,$password,$server_file,$xmlFile);

The file must be downloaded and the then I will convert it into XML for further processing. 该文件必须下载,然后我将其转换为XML进行进一步处理。 Note: The file is downloading perfect on localhost , this problem occurs as we put it on live server. 注意:该文件正在localhost上完美下载,当我们将其放在实时服务器上时会发生此问题。

GZ file may not only have text in it . GZ文件中可能不仅包含文本。 So you have to download it with binary mode so that anything containing in it could be download successfully 因此,您必须以二进制模式下载它,以便其中包含的所有内容都可以成功下载

if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII)) {
    echo "Successfully written to $local_file.";
    //   exit;

} else {
    echo "Error downloading $server_file.";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM