Skip to content

Instantly share code, notes, and snippets.

@Ninpo
Created January 24, 2019 01:09
Show Gist options
  • Save Ninpo/5f96a1f578395ebceefe32ae45e7ed5c to your computer and use it in GitHub Desktop.
Save Ninpo/5f96a1f578395ebceefe32ae45e7ed5c to your computer and use it in GitHub Desktop.
$ftp_conn = false;
$errors = array();
$leave = false;
$putFile = false;
$retries = 0;
$maxRetries = 10;
$remoteFilename = $mailoutID.'.doc';
$ftpHost = getenv('PDF_CONVERTER_HOST');
$ftpUser = getenv('PDF_CONVERTER_USER');
$ftpPass = getenv('PDF_CONVERTER_PASSWORD');
$ftpDir = getenv('PDF_CONVERTER_PATH');
// keep trying until connected
while (!$leave)
{
$error = "";
$ftp_conn = @ftp_connect($ftpHost);
if ($ftp_conn)
{
array_push($errors, "Connected");
$ftp_login = @ftp_login($ftp_conn, $ftpUser, $ftpPass);
if ($ftp_login)
{
ftp_pasv($ftp_conn, true);
array_push($errors, "Logged In");
if (@ftp_chdir($ftp_conn, $ftpDir.'/uploading'))
{
array_push($errors, "Changed Dir");
if (@ftp_put($ftp_conn, $remoteFilename, $concatFile, FTP_BINARY))
{
if (ftp_rename($ftp_conn, $remoteFilename, '/'.$ftpDir.'/'.$remoteFilename))
{
$leave = true;
$putFile = true;
$retries = 0;
}
else
{
array_push($errors, "Failed to rename file to ".$ftpDir);
$leave = true;
}
}
else
{
array_push($errors, "Failed to put file to ".$ftpDir);
}
@ftp_close($ftp_conn);
}
else
{
@ftp_close($ftp_conn);
array_push($errors, "Failed to change directory to ".$ftpDir);
}
}
else
{
@ftp_close($ftp_conn);
array_push($errors, "Failed to login to FTP using user ".$ftpUser);
}
}
else
{
array_push($errors, "Failed to connect to FTP host ".$ftpHost);
}
// we're not connected sleep for a little while then try again
if (!$putFile)
{
++$retries;
if ($retries == $maxRetries)
{
$leave = true;
array_push($errors, $error." - Maximum retries (".$maxRetries.") met");
}
else
{
sleep(5);
array_push($errors, $error." - Retry ".$retries);
}
}
else
{
$errors = array();
}
} // end while not leave
unlink($concatFile);
//mail("idiot@redacted.com", "test", print_r($argv, true)."\n\n".print_r($errors, true)."\n\n".$templateContent."\n\n".$templateHeaderContent."\n\n".$mainContent."\n\n".$templateFooterContent);
//
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment