将文件上传到ftp时出错

时间:2013-03-07 09:48:12

标签: php file-upload ftp

我想将csv Feed上传到公共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

我做错了什么?

2 个答案:

答案 0 :(得分:1)

这可能是因为FTP的工作方式,与网络布局冲突。

虽然您连接到FTP服务器以控制会话,但默认情况下,服务器会“回调”给您,并为传输提供单独的数据连接。在某些情况下,防火墙,NAT等都可能会干扰此过程。

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

答案 1 :(得分: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);
相关问题