I'm using the following code to download a file via FTP in PHP:
$fp = fopen($local_file, 'w+');
$conn_id = ftp_connect($host);
$login_result = ftp_login($conn_id, $user, $pass);
$ret = ftp_nb_fget($conn_id, $fp, $remote_file, FTP_BINARY);
while ($ret == FTP_MOREDATA) {
$ret = ftp_nb_continue($conn_id);
}
if ($ret != FTP_FINISHED) {
echo "<span style='color:red;'><b>There was an error downloading the file!</b></span><br>";
logThis("log.txt", date('h:i:sa'), "ERROR DOWNLOADING FILE!");
exit();
}
fclose($fp);
<<php code continues below this....>>
This code seems to be working fine. The file is downloaded, and the MD5 hash of the file matches the hash of the file on the other server before it was downloaded. So the download does complete.
In any event, using that code above, even with the file successfully downloading, it's hitting the code inside of the if ($ret != FTP_FINISHED) condition.
If the file downloads fine, why is FTP_FINISHED not true?
EDIT
When I check the value of $ret after the WHILE loop, the times the script completes fine $ret=1 and the times the script fails $ret=0
However, there are times when the script fails because $ret=0 when the file is actually downloaded properly, which can be confirmed with a MD5 comparison.
Also, 0 or 1 are not values that should be returned from these commands. The official PHP documentation give three possible return values, they are FTP_FAILED or FTP_FINISHED or FTP_MOREDATA