2

My script packs some files with ZIP then uploads this ZIP to another server. After the upload it checks size of the ZIP on the FTP and locally. If sizes are the same- the local ZIP is deleted.

The problem is ftp_size() returns -1. But only in the real script. In my test script it works fine.

Test script works like this:

$f = ftp_connect(HOST);
$res = ftp_login($f, USER, PASS); 
$fname = 'archive_2018-09-18_13-39';
$fsize = ftp_size($f, "$fname-img.zip").''; 
$fsize2 = filesize("backup/$fname-img.zip").''; 
echo $fsize . '<br>' . $fsize2;

and it returns:

22907946995
22907946995

The real script works like this:

$f = ftp_connect(HOST);
$res = ftp_login($f, USER, PASS); 
$fname = 'archive_' . date('Y-m-d_H-i');

exec("zip -r -0 backup/$fname-img.zip  \"website\" 2>&1");
exec('curl -T "' . "backup/$fname-img.zip" . '" ftp://' . HOST . ' --user ' . USER . ':' . PASS . ' 2>&1');

$fsize = ftp_size($f, "$fname-img.zip").''; 
$fsize2 = filesize("backup/$fname-img.zip").''; 
echo $fsize . '<br>' . $fsize2;

and it shows:

-1
22907946995

Real script uploads file just fine. It just doesn't show correct size on the FTP server. So it's not a problem with size of the file and not a problem with FTP connection.

9
  • Why not use ftp_put() to upload the file? Oh, and -1 probably just means the file, in the checked directory, doesn't exist. Commented Sep 18, 2018 at 12:57
  • If you get -1, try and call error_get_last() and log it to check if that gives you any clues. Commented Sep 18, 2018 at 12:58
  • Your title says "sometimes". While from your question text, it seems that you get -1 always (in the real script). Please elaborate on this. Commented Sep 18, 2018 at 13:23
  • @Magnus Eriksson error_get_last() returned nothing but I still got -1.
    – Tom
    Commented Sep 18, 2018 at 13:34
  • Do you have an access to FTP server log file? Can you post it? Or can do a wireshark capture of the FTP session? Commented Sep 18, 2018 at 13:46

1 Answer 1

1

Try moving ftp_connect only after the call to curl. There is possibly some caching involved that prevents the FTP server from returning correct size immediately, if the file is uploaded using a different connection.


Though I'd strongly suggest you to use PHP functions to upload the file.

Not the answer you're looking for? Browse other questions tagged or ask your own question.