Jump to content

loop to repeat ftp connect and get file


madwoman

Recommended Posts

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.

Link to comment
Share on other sites

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
}
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.