Translate

If you're looking for a post about Cait Riley, click here
>

Wednesday 12 October 2016

FtpGetFile bug and resolution

I was using FtpGetFile in some VBA to download some files from an FTP server but I was losing data from the end of the files. Downloading the same files with an FTP client got the complete files - no problem.

The files concerned got onto the server via some PHP on a web server that opened a file for append, wrote some data, closed it and  sometime later did it again (and again) like this:

$myfile = fopen("Afile.txt", "a") or die("Unable to open file!");
fwrite($myfile, $stuff1."\r\n");
fwrite($myfile, $stuff2."\r\n");
...
fwrite($myfile, "stuffN"."\r\n");
fclose($myfile);
 

(up to an hour passes)
 

$myfile = fopen("Afile.txt", "a") or die("Unable to open file!");
fwrite($myfile, $stuff11."\r\n");
fwrite($myfile, $stuff12."\r\n");

...
fclose($myfile);

I noticed that the incomplete files always had complete sessions/records - just not enough of them while the FTP client got them all.

Eventually I dug into the documentation for  FtpGetFile  and found there was a flag INTERNET_FLAG_RELOAD I could set which "Forces a download of the requested file, object, or directory listing from the origin server, not from the cache."

That fixed it :-)