dongwang6837 2013-06-13 14:30
浏览 140
已采纳

将csv文件上传到ftp服务器 - 文件为空

I am trying to store a file from my local server on an ftp server.

source file is in my current directory (same as php file)

I run the script with http://www.server.co.za/kisv2/xmltest/export.php

file to upload to ftp is http://www.server.co.za/kisv2/xmltest/exportfile.csv

destination ftp path is: ftp://ftp.ftpserver.co.za/LocExports/exportfile.csv

my ftp login defaults to: ftp://ftp.ftpserver.co.za

so I want to copy file from in current directory exportfile.csv to ftp://ftp.ftpserver.co.za/LocExports/exportfile.csv

My Current syntax is:

$source = 'exportfile.csv';  //this is a file in the same directory as my php file. full path is... http://www.server.co.za/kisv2/xmltest/exportfile.csv
$target = '/LocExports/exportfile.csv'; //full path is... ftp://ftp.ftpserver.co.za/LocExports/exportfile.csv

$conn = ftp_connect("ftp.ftpserver.co.za") or die("Could not connect");
ftp_login($conn, "username", "password");

$upload = ftp_put($conn, $target, $source, FTP_ASCII);
if (!$upload) { echo 'FTP upload failed!'; }

echo "complete";

This gives me error Warning: ftp_put() [function.ftp-put]: Opening ASCII mode data connection. the file does appear on the FTP server but is blank and 0bytes in size.

Any ideas welcome.

Thanks and regards

UPDATE

$source = 'exportfile.csv';  
$target = '/LocExports/exportfile.csv'; 

$conn = ftp_connect("ftp.server.co.za") or die("Could not connect");
ftp_login($conn, "username", "password");

ftp_pasv($conn, true); 

$upload = ftp_put($conn, $target, $source, FTP_BINARY);
if (!$upload) { echo 'FTP upload failed!'; }

echo "complete";

this still fails with: Warning: ftp_put() [function.ftp-put]: Opening BINARY mode data connection.

file is created on ftp but empty.

thanks again

  • 写回答

2条回答 默认 最新

  • duanrebo3559 2013-06-13 14:33
    关注

    Add ftp_pasv($conn, true); after your ftp_login(...) .

    From http://www.php.net/manual/en/function.ftp-put.php

    If when using ftp_put you get the one of the following errors:

    Warning: ftp_put() [function.ftp-put]: Opening ASCII mode data connection

    Warning: ftp_put() [function.ftp-put]: Opening BINARY mode data connection

    and it creates the file in the correct location but is a 0kb file and all FTP commands thereafter fail. It is likely that the client is behind a firewall. To rectify this use:

    ftp_pasv($conn, true);

    Before executing any put commands. Took me so long to figure this out I actually cheered when I did :D

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dsvq5069 2013-10-14 13:09
    关注

    I was having the same problem. The file created but size 0 KB. after setting mode to passive, my file successfully transferred to FTP Server. InFact there are 3 things we have to take care while uploading file on FTP Server.

    1. set file type to BINARY: objFtpClient.setFileType(FTP.BINARY_FILE_TYPE);

    2. set File Transfer Mode to BINARY: objFtpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);

    3. set Mode to Passive (i dont know what it does internally. but it works...!): objFtpClient.enterLocalPassiveMode();

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 I350 Gigabit Network
  • ¥15 关于#abap#的问题,请各位专家解答!
  • ¥20 内网通过公网访问外网问题
  • ¥20 谁有这个东西 继续教育的
  • ¥15 怎么使请求通过cors
  • ¥15 WDM 驱动ACPI 相关疑问
  • ¥15 prism 跨窗体共享数据绑定 wpf
  • ¥15 hdl designer突然用不了系统的moduleware组件,请问有人遇到或者怎么解决吗?
  • ¥15 0基础计算机毕设,应该从哪开始?
  • ¥60 使用DKT40脑图划分ROI区域