ftp_put uploads an empty file
Asked Answered
T

7

5

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 status
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.

Titanothere answered 20/7, 2010 at 14:16 Comment(1)
The output from running this is: Connected to FTP Server Warning: ftp_put() [function.ftp-put]: Type set to I. in D:\inetpub\wwwroot\website.com\html\upload.php on line 19 (the line with $upload = ftp_put) FTP upload has failed!Titanothere
T
1

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)

Titanothere answered 21/7, 2010 at 10:25 Comment(0)
C
16

Try transferring the file with passive mode enabled:

Passive Mode

Cephalonia answered 20/7, 2010 at 14:35 Comment(2)
I am getting a PHP warning for: "Warning: ftp_put() [function.ftp-put]: php_connect_nonb() failed: No such file or directory (2)" for the line where ftp_put is after adding passive modeTitanothere
passive mode actually stopped the empty file being created, but obviously gave up the error instead. :(Titanothere
F
8

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.

Fractious answered 20/7, 2010 at 14:31 Comment(1)
i was uploading images by this function with ascii mode and images are uploaded with right size but files are corrupted i used FTP_Binary and all went well. thanks KellyUnderwear
H
4

turn passive mode on

  ftp_pasv($conn_id, true);
Howrah answered 8/5, 2017 at 11:33 Comment(0)
T
1

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)

Titanothere answered 21/7, 2010 at 10:25 Comment(0)
R
0

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

ftp_pasv($conn_id, true);

the file was uploaded perfectly.

Retentive answered 11/1, 2019 at 11:5 Comment(0)
M
0

Turning the passive mode on did the trick for me.

ftp_pasv($conn_id, true);
Meemeece answered 6/6, 2020 at 21:32 Comment(0)
K
0

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.

Kachine answered 6/12, 2022 at 15:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.