954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

FTP Script that keeps retrying on failure

Hi

I have a script that is scheduled to run with task scheduler will download a file via FTP and import it to a mysql DB
How would I go about handling the retries if there is either a problem with the connection or something.

I tried going about it using the sleep(); function and using header("Location:file2.php")
then redirecting it back to the original thus causing a loop.

The reason I went about it this way is that if I just used a loop in the original page then it detected that eventually timed out.

What is the best way to go about it?

fabzster
Light Poster
40 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

This loop won't timeout:

<?php
while(1)
{
   # run ftp connection

   # check for file
   if(file_exists('file.ext'))
   {
      break;
   }
   pause(10);
}
?>

You can also control ftp timeout, check this: http://www.php.net/manual/en/function.ftp-set-option.php

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

this is the script I created but PHP eventually times out

<?php

//Include PHP Mailer Class
require("class.phpmailer.php");

$LogDateTime = date("Y-m-d H:i:s");
$date = date("Y-m-d");

$date = date("Y-m-d");

$ftp_server = "FTP_Address";
$ftp_user_name = "FTP_UserName";
$ftp_user_pass = "FTP_Password";

$local_file = './downloaded/CellUpload'.$date.'.xls';
$server_file = 'CellUpload'.$date.'.xls';

// set up basic connection
$conn_id = ftp_connect($ftp_server); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// check connection
if ((!$conn_id) || (!$login_result))
    { 
        $LogDateTime = date("Y-m-d H:i:s");
        //write data to logfile
        $handle = fopen("./logs/log.txt", "a");
        fwrite($handle,""."$LogDateTime"." "."[FTP CONNECT ERROR]"." "."Failed To Connect To"." "."$ftp_server"."\r\n");
        fclose($handle);
        
        $LogDateTime = date("Y-m-d H:i:s");
        //write data to logfile
        $handle = fopen("./logs/log.txt", "a");
        fwrite($handle,""."$LogDateTime"." "."[FTP SLEEP]"." "."Waiting 60 Seconds To Retry Connection To"." "."$ftp_server"."\r\n");
        fclose($handle);
 
        sleep(60);
        header("Location:ftp_script2.php");
           
        exit; 
    }
    else
        {
            $LogDateTime = date("Y-m-d H:i:s");
            
            //write data to logfile
            $handle = fopen("./logs/log.txt", "a");
            fwrite($handle,"\r\n"."$LogDateTime"." "."[FTP CONNECTED]"." "."Connect Successfully To"." "."$ftp_server"."\r\n");
            fclose($handle);
        }

// Change The Directory
if (ftp_chdir($conn_id, "/public_html/reports/exports"))
    {
        $LogDateTime = date("Y-m-d H:i:s");
        //write data to logfile
        $handle = fopen("./logs/log.txt", "a");
        fwrite($handle,""."$LogDateTime"." "."[FTP CHANGED DIRECTORY]"." "."Current Directory Is Now:"."(./public_html/reports/exports)"."\r\n");
        fclose($handle);
    }
else
    { 
        $LogDateTime = date("Y-m-d H:i:s");
        //write data to logfile
        $handle = fopen("./logs/log.txt", "a");
        fwrite($handle,"\r\n"."$LogDateTime"." "."[FTP CHANGED DIRECTORY ERROR]"."Failed To Change Directory To:"."(./public_html/reports/exports)"."\r\n");
        fclose($handle);
        exit;
    }

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY))
    {
        $LogDateTime = date("Y-m-d H:i:s");
        //write data to logfile
        $handle = fopen("./logs/log.txt", "a");
        fwrite($handle,""."$LogDateTime"." "."[FTP DOWNLOAD]"." "."File Successfully Downloaded To:"." "."($local_file)"."\r\n");
        fclose($handle);
    }
else
    {
        $LogDateTime = date("Y-m-d H:i:s");
        //write data to logfile
        $handle = fopen("./logs/log.txt", "a");
        fwrite($handle,"".$LogDateTime." "."[FTP DOWNLOAD ERROR]"." "."Failed To Download File:"." "."($local_file)"."\r\n");
        fclose($handle);
        exit;
    }

// close the FTP stream 
ftp_close($conn_id);
fabzster
Light Poster
40 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: