FTP Synchronization utilizing PHP

  • Thread starter Thread starter IRJosh
  • Start date Start date
I

IRJosh

Scenario:

We are trying to set up a script that will run from an Amazon EC2 instance that will grab all incoming files that come through an FTP on the same instance, and also a different FTP. This script will also need to grab files from a different FTP when they come in, which is at random intervals. Currently there is a script written in php that accomplishes some of this but it was not written by myself. The goal here is to synchronize the EC2 with both of the different FTP sites we have set up. These scripts will be running from the EC2 instance. After the files are brought over they are being decrypted with gnupg. I have been hunting for codes or snippets but have been relatively unsuccessful. I have the current script I can show - omitting password credentials of course. I am really trying to implement this ASAP - and possibly this is something that can be achieved by adding a few lines to our current code. Please help!!


Code:
function sendFile($targetHost,$userName,$passWord,$remoteDirPath,$localDirPath,$fileName) {
$fh = fopen($localDirPath.'/'.$fileName,'r');
if ($fh === false) {
die("we couldn't open ".$localDirPath.'/'.$fileName);
}
if (!isset($localDirPath)) {
die("you must specify a directory path");
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $targetHost.'/'.$remoteDirPath.'/'.$fileName);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fh);
//SSL stuff
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_DEFAULT);
curl_setopt($ch, CURLOPT_PORT, 21);
curl_setopt($ch, CURLOPT_USERPWD, $userName.':'.$passWord);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localDirPath.'/'.$fileName));
curl_setopt($ch, CURLOPT_FTP_CREATE_MISSING_DIRS,true);
//curl_setopt($ch, CURLOPT_FILETIME,true);
$result = curl_exec ($ch);
$error = curl_errno($ch);
if ($error) {
$errText = curl_strerror($error);
echo "Error Sending: $errText".PHP_EOL;
exit;
}
fclose($fh);
}

function getFile($targetHost,$userName,$passWord,$remoteDirPath,$localDirPath,$fileName) {
global $epochSecs;
$ch = curl_init();
$fh = fopen($localDirPath.'/'.$fileName,'w');
if (!isset($localDirPath)) {
die("you must specify a directory path");
}
curl_setopt($ch, CURLOPT_URL, $targetHost.'/'.$remoteDirPath.'/'.$fileName);
curl_setopt($ch, CURLOPT_INFILE, $fh);
//SSL stuff
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_DEFAULT);
curl_setopt($ch, CURLOPT_PORT, 21);
curl_setopt($ch, CURLOPT_USERPWD, $userName.':'.$passWord);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILETIME,true);
$result = curl_exec ($ch);
$error = curl_errno($ch);
if ($error) {
$errText = curl_strerror($error);
echo "Error Fetching: $errText".PHP_EOL;
exit;
}
$epochSecs = curl_getinfo($ch,CURLINFO_FILETIME);
fwrite($fh,$result);
fclose($fh);
}

$fileNameBase = '{INSERT HERE}';
$fileNamesSuffixesArray = array('array goes here');

//$res = gnupg_init();
//gnupg_import($res,'HERE');
//gnupg_adddecryptkey ( $res , 'HERE' , 'HERE' );

foreach ($fileNamesSuffixesArray as $suffix) {
$fileName = $fileNameBase.$suffix.'.csv.pgp';
//call to get file needs to be fleshed out with proper arguments
echo "downloading $fileName".PHP_EOL;
getFile( 'FTP SITE HERE'
, 'USERNAME'
, 'PASS'
, 'CLIENT'
, 'FOLDER LOCATION'
, $fileName
);
$outputFileName = $fileNameBase.$suffix.'.csv';
echo "about to decrypt $fileName to $outputFileName\n";
shell_exec("gpg --batch --passphrase PASS\KEYNAME --output (output location)");
touch('file directory'.$outputFileName,$epochSecs);
touch('file directory'.$fileName,$epochSecs);
//$encodedData = file_get_contents('file location'.$fileName);
//file_put_contents('file location'.$outputFileName,gnupg_decrypt($res,$encodedData));
//$decryptError = gnupg_geterror($res);
//if ($decryptError !== false) {
// echo "Error: $decryptError\n";
// exit;
//}
//unset($encodedData);
echo "sending csv file to DOMO upload directory".PHP_EOL;
sendFile( 'ftp location' // ftp:// + actual ftp hostname
, 'username' // username
, 'pass' // duh
, 'location' //directory path on ftp server where the file of interest is to reside
, 'directory path' //directory path where a file is to be uploaded from
, $outputFileName //name of file to upload
);
$filePath = 'location';
$dateStamp = "";
$dow = date('N');
if ((int)$dow === 4) {
echo "dow is 4\n";
$dateStamp = date('m-d-Y');
}
else if ((int)$dow > 4) {
echo "dow is > 4\n";
$daysToSubtract = $dow - 4;
$intervalString = 'P'.$daysToSubtract.'D';
$di = new DateInterval($intervalString);
$di->invert = 1;
$today = new DateTime("now");
$today->add($di);
$dateStamp = $today->format('m-d-Y');
}
else {
echo "dow is < 4\n";
$daysToSubtract = 7 - $dow;
$intervalString = 'P'.$daysToSubtract.'D';
$di = new DateInterval($intervalString);
$di->invert = 1;
$today = new DateTime("now");
$today->add($di);
$dateStamp = $today->format('m-d-Y');
}
$timeStamp = date('Y-m-d H.i.s',$epochSecs);
$dateStampedCSV = $fileNameBase.$suffix.' - '.$timeStamp.'.csv';
rename($filePath.'/'.$outputFileName,$filePath.'/'.$dateStampedCSV);
touch($filePath.'/'.$dateStampedCSV,$epochSecs);
echo "sending csv file _____$dateStamp".PHP_EOL;
sendFile( 'ftp site' // ftp:// + actual ftp hostname
, 'username' // username
, 'pass' // duh
, '/location '.$dateStamp //directory path on ftp server where the file of interest is to reside
, 'location' //directory path where a file is to be uploaded from
, $dateStampedCSV //name of file to upload
);
$pgpFileName = $fileNameBase.$suffix.'.csv.pgp';
$datedPGPFileName = $fileNameBase.$suffix.' - '.$timeStamp.'.csv.pgp';
rename($filePath.'/'.$pgpFileName,$filePath.'/'.$datedPGPFileName);
touch($filePath.'/'.$datePGPFileName,$epochSecs);
echo "sending PGP encrypted file to here $dateStamp".PHP_EOL;
sendFile( 'FTP HERE' // ftp:// + actual ftp hostname
, 'username' // username
, 'pass' // duh
, 'location '.$dateStamp //directory path on ftp server where the file of interest is to reside
, 'staging' //directory path where a file is to be uploaded from
, $datedPGPFileName //name of file to upload

Continue reading...
 
Back
Top