2010-07-20 14:16



I'm trying to upload a file via ftp_put to a windows server.

my code is as follows:

    $date           = date('ymd');
    $file_name      = $date.'.csv';
    $file_location  = 'D:/inetpub/wwwroot/'.$file_name;

//set up basic connection
$conn_id = ftp_connect(FTP_HOST, FTP_PORT);

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

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!"; 
}  else { 
    echo "Connected to FTP Server";

$upload = ftp_put($conn_id, $file_name, $file_location, FTP_ASCII);

// check upload status
if (!$upload) { 
    echo "FTP upload has failed!"; 
} else { 
    echo "File Uploaded";

// close the FTP stream 

If I upload the file manually using filezilla, it works perfectly. If I use the code above, it creates an empty file.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • donglinxia1541 donglinxia1541 11年前

    It turns out that UKFast was blocking the connection and transfer. (They also require it to be Active Mode only).

    Now they've unblocked it, it's working perfectly. (Before it seemed to just time out)

    点赞 评论 复制链接分享
  • duanlong4890 duanlong4890 2年前

    thanks "Khan Muhammad" for your answer, when I added this part :

    ftp_pasv($conn_id, true);

    the file was uploaded perfectly.

    点赞 评论 复制链接分享
  • dongtai419309 dongtai419309 11年前

    Try transferring the file with passive mode enabled:

    Passive Mode

    点赞 评论 复制链接分享
  • dongpin4611 dongpin4611 11年前

    try using FTP_BINARY instead of FTP_ASCII like this.

    $upload = ftp_put($conn_id, $file_name, $file_location, FTP_BINARY);

    PHP ftp can be buggy but I have found that it pretty much works in binary transfer mode.

    点赞 评论 复制链接分享
  • duanfu3390 duanfu3390 4年前

    turn passive mode on

      ftp_pasv($conn_id, true);
    点赞 评论 复制链接分享