2

我想将 csv 提要上传到公共 ftp。

我有这个:

global $_CONFIG;
$host           = $_CONFIG['po']['ftp']['server'];
$username       = $_CONFIG['po']['ftp']['username'];
$password       = $_CONFIG['po']['ftp']['password'];
$ftp_path       = $_CONFIG['po']['ftp']['upload_path'];

$file = $export_file;
$fp = fopen($export_file, 'r+');

// set up basic connection
$conn_id = ftp_connect($host);

// login with username and password
$login_result = ftp_login($conn_id, $username, $password);

// try to upload $file
if (ftp_fput($conn_id, $ftp_path, $fp, FTP_BINARY)) {
    echo "Successfully uploaded $file\n";
    exit;
} else {
    echo "There was a problem while uploading $file\n";
    exit;
}
// close the connection and the file handler
fclose($fp);

ftp_close($conn_id);

主机、用户、密码正确。导出文件是D:/some path .csv

问题是调用了 ftp 服务器上的文件.upload.somefile.csv(并且它没有完整大小)。

ftp_path 是/somefile.csv.

我究竟做错了什么?

4

2 回答 2

1

这可能是由于 FTP 的工作方式与您的网络布局冲突而发生的。

尽管您连接到 FTP 服务器来控制会话,但默认情况下,服务器会通过单独的数据连接“回调”给您进行传输。在某些情况下,防火墙、NAT 等都可能会干扰此过程。

尝试使用 FTP 的PASV模式,使用ftp_pasv,它会反转数据连接的方向。

于 2013-03-07T09:59:32.290 回答
0

文件csv应该使用FTP_ASCII,而不是FTP_BINARY

所以:

  global $_CONFIG;
    $host           = $_CONFIG['po']['ftp']['server'];
    $username       = $_CONFIG['po']['ftp']['username'];
    $password       = $_CONFIG['po']['ftp']['password'];
    $ftp_path       = $_CONFIG['po']['ftp']['upload_path'];

        $file = $export_file;
        $fp = fopen($export_file, 'r+');

        // set up basic connection
        $conn_id = ftp_connect($host);

        // login with username and password
        $login_result = ftp_login($conn_id, $username, $password);

        // try to upload $file
        if (ftp_fput($conn_id, $ftp_path, $fp, FTP_ASCII)) {
            echo "Successfully uploaded $file\n";
            exit;
        } else {
            echo "There was a problem while uploading $file\n";
            exit;
        }
        // close the connection and the file handler
        fclose($fp);

        ftp_close($conn_id);
于 2013-03-07T09:52:52.237 回答