Hi
I am using the following to connect to an external ftp site to collect a txt file which then gets imported into a mysql table. The connection seems a little hit and miss and I want to set up a loop through until the action is successful - can anyone advise how I would add this to my existing script - I have tried a couple of options without success. The existing code is as follows:
<?php
$today=(gmdate("dmyGi") . "");
// define some variables
$local_file = 'IncomingFiles-' . $today . '.txt';
$server_file = 'files.txt';
// set up basic connection
$ftp_server="xxx.ftpserver.xxx";
$conn_id = ftp_connect($ftp_server);
// login with username and password
$ftp_user_name="1234567";
$ftp_user_pass="1234567";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_ASCII)) {
echo "successfully written to filesincoming\n";
} else {
echo "there was a problem connecting to the server for the collection of filesincoming";
exit;
}
I think the issue may be one of the connection timing out so I am not sure whether setting a longish timeout would be a better option but I though that using a loop would allow me to rely more on the script actually completing when I run it as a CRON job eventually. At the moment the run fails to connect about once in every 3 attempts.
Any advice would be appreciated.