繁体   English   中英

通过FTP下载压缩文件

[英]Downloading a gunzipped file over FTP

我正在尝试从FTP服务器下载压缩文件。

它似乎下载成功(正确的文件大小等),但是当我提取内容时,它失败,表明违反了数据格式。

如果我使用FileZilla这样的FTP客户端手动下载了相同的文件,然后将其解压缩,则解压缩有效,这意味着我的PHP用于下载文件的方式不正确。

这是我的代码:

$this->_file = 'data.csv.gz';
$this->_directory = DOC_ROOT.'/imports/';

private function _loadFromFtpDataSource($url=null,$username=null,$password=null) {
    try {
        $conn_id = ftp_connect($url);
        $login_result = ftp_login($conn_id, $username, password);
        ftp_pasv($conn_id, true);
        $handle = fopen($this->_directory . $this->_file, "w");
        ftp_fget($conn_id, $handle, $this->_file, FTP_ASCII, 0);            
        ftp_close($conn_id);
        fclose($handle);
    } catch (Exception $e) {
        $this->status = false;
        error_log("Failed to connect to ftp server");
    }
}

谁能看到任何原因导致下载不正确? 通过FTP下载压缩文件时是否需要特别注意?

尝试更改此行:

ftp_fget($conn_id, $handle, $this->_file, FTP_ASCII, 0);

ftp_fget($conn_id, $handle, $this->_file, FTP_BINARY, 0);

您正在传输二进制数据存档( ...when I extract the contents... ),而不是文本文件
阅读更多
http://www.coreftp.com/docs/web1/Ascii_vs_Binary_transfers.htm

如果文件不使用纯ASCII(例如,改用UTF-8),则您的下载很可能会损坏。 如果将模式从FTP_ASCII更改为FTP_BINARY,应该没问题。

二进制文件需要以binary模式而非ascii模式下载

$this->_file = 'data.csv.gz';
$this->_directory = DOC_ROOT.'/imports/';

private function _loadFromFtpDataSource($url=null,$username=null,$password=null) {
    try {
        $conn_id = ftp_connect($url);
        $login_result = ftp_login($conn_id, $username, password);
        ftp_pasv($conn_id, true);
        $handle = fopen($this->_directory . $this->_file, "w");
        ftp_fget($conn_id, $handle, $this->_file, FTP_BINARY, 0);            
        ftp_close($conn_id);
        fclose($handle);
    } catch (Exception $e) {
        $this->status = false;
        error_log("Failed to connect to ftp server");
    }
}

暂无
暂无

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

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