madwoman Posted September 22, 2013 Share Posted September 22, 2013 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. Quote Link to comment Share on other sites More sharing options...
.josh Posted September 22, 2013 Share Posted September 22, 2013 IMO it's not a good idea to let your script keep trying to connect indefinitely. Instead, you should rewrite your stuff to gracefully fail and log the fail. But I think it's okay to try a few times before gracefully failing. You can do like: $attempts = 0; $maxAttempts = 10; while ($attempts < $maxAttempts) { $conn_id = ftp_connect($ftp_server); if ($conn_id) break; $attempts++; } if (!$conn_id) { // do stuff to gracefully fail } else { // good to go, do stuff } Quote Link to comment Share on other sites More sharing options...
madwoman Posted September 23, 2013 Author Share Posted September 23, 2013 Hi Thanks for the suggestion, I agree about using a limited number of attempts - am I right in assuming that I need to plug this bit of code in after line 16 of my initial code? Cheers Quote Link to comment Share on other sites More sharing options...
.josh Posted September 23, 2013 Share Posted September 23, 2013 lines 1-7 in my code would replace your line #10, since you said it occasionally hangs up on the initial connect. Lines 8-12 in my code is an example of what you'd do afterwards. Basically all the rest of your code would go inside the if part of the if..else, and whatever you want to do to gracefully fail would go inside the else Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.