开发者

ftp_put uploads an empty file

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/website.com/html/assets/'.$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!"; 
    exit;
}  else { 
    echo "Connected to FTP Server";
}

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

// check upload s开发者_运维百科tatus
if (!$upload) { 
    echo "FTP upload has failed!"; 
} else { 
    echo "File Uploaded";
}

// close the FTP stream 
ftp_close($conn_id); 

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


Try transferring the file with passive mode enabled:

Passive Mode


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.


turn passive mode on

  ftp_pasv($conn_id, true);


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)


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

ftp_pasv($conn_id, true);

the file was uploaded perfectly.


Turning the passive mode on did the trick for me.

ftp_pasv($conn_id, true);


I just figured out that "foreach" loop was creating 0 bytes files, while "for" loop was succesfully creating valid files.

$arr = array(
  "1.pdf",
  "2.pdf",
  "3.pdf",
);

Does not work :

foreach($arr as $file) {
  $upload = ftp_nb_put($ftp, $file, $file, FTP_BINARY, FTP_AUTORESUME); 
  while (FTP_MOREDATA == $upload){
    $upload = ftp_nb_continue($ftp);
  }
  if ($upload != FTP_FINISHED) {
    echo "Error with : ".$fichier;
  }
}

Works:

for($i = 0; $i<count($arr); $i++) {
  $upload = ftp_nb_put($ftp, $arr[$i], $arr[$i], FTP_BINARY, FTP_AUTORESUME);   
  while (FTP_MOREDATA == $upload){
    $upload = ftp_nb_continue($ftp);
  }
  if ($upload != FTP_FINISHED) {
    echo "Error with : ".$arr[$i];
  }
}

If ever it could help ... But I'm curious why tho.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜