繁体   English   中英

将文件上传到ftp时出错

[英]error while uploading a file to a ftp

我想将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

我究竟做错了什么?

发生这种情况的原因是FTP的工作方式,与您的网络布局冲突。

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

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

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);

暂无
暂无

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

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