0

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?

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by fabzster
0

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);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.