- - - - - ERROR - - - - -

"; print $errmsg; print "

- - - - - ERROR - - - - -
"; print ""; die; } // end c_die() function ParsedFTPListing($dirlist) { // split the results of ftp_rawlist into something palatable $i = 0; foreach ($dirlist as $current) { $split = preg_split("[ ]", $current, 9, PREG_SPLIT_NO_EMPTY); if ($split[0] != "total") { $parsed[$i]['isdir'] = $split[0]{0} === "d"; $parsed[$i]['perms'] = $split[0]; $parsed[$i]['number'] = $split[1]; $parsed[$i]['owner'] = $split[2]; $parsed[$i]['group'] = $split[3]; $parsed[$i]['size'] = $split[4]; $parsed[$i]['month'] = $split[5]; $parsed[$i]['day'] = $split[6]; $parsed[$i]['time/year'] = $split[7]; $parsed[$i]['name'] = $split[8]; $i++; } } return $parsed; } // end ParsedFTPListing() function DirIsGood($CheckDirName) { // We skip some directories $RetVal = true; if ($CheckDirName == '.') { $RetVal = false; } // neverending recursion is bad if ($CheckDirName == '..') { $RetVal = false; } // neverending recursion is bad if ($CheckDirName == 'blogger') { $RetVal = false; } // blogger directories maintained by blogger, don't mess return $RetVal; } function FileIsGood($CheckFileName) { // We skip some files $RetVal = true; if ($CheckFileName == 'bob') { $RetVal = false; } // sample return $RetVal; } function FileShouldChmod($CheckFileName) { // We chmod some files $RetVal = false; if ($CheckFileName == 'goodprog01.pl') { $RetVal = true; } if ($CheckFileName == 'goodprog02.pl') { $RetVal = true; } if ($CheckFileName == 'goodprog03.cgi') { $RetVal = true; } if ($CheckFileName == 'goodprog04.cgi') { $RetVal = true; } return $RetVal; } function FileIsOnFTP($CheckFile, $FTPArray) { $RetVal = false; for ($i=0; $i'; flush(); // replace w/clog @mkdir($LocalDir, 0777); // Loop thru the dest directory for ($i=0; $i'; flush(); // replace w/clog } } } } // end loop thru dest dir } // end IterateThisDir_ZipDest() /******* Recursive loop, pass 2; iterate thru source dir, push changes 'up' to dest *****************************/ function IterateThisDir_Source($conn, $SourceDir, $DestinationDir, $Indent) { // recursive! // Get list of source directory - stash in $Source_DirArray $Source_DirArray = scandir($SourceDir); // Get list of destination directory - stash in $Dest_DirArray $Dest_DirList = ftp_rawlist($conn, $DestinationDir); $Dest_DirArray = ParsedFTPListing($Dest_DirList); // Loop thru the source directory for ($i=0; $i'; flush(); // replace w/clog ftp_chdir($conn, $Dest_Object_FullPath); } // and then recurse into the directory to get it updated IterateThisDir_Source($conn, $Source_Object_FullPath, $Dest_Object_FullPath, $Indent . '   '); ftp_chdir($conn, $DestinationDir); // pop back up } } // If this is a file, then ... if (is_file($Source_Object_FullPath)) { // Check for file in the skip list if (FileIsGood($Source_Object)) { if (FileIsOnFTP($Dest_Object, $Dest_DirArray)) { // if on the FTP site then // if source newer then dest, upload $Source_Object_TimeStamp = filemtime($Source_Object_FullPath); $Dest_Object_TimeStamp = strtotime(FileDateTimeStampOnFTP($Dest_Object, $Dest_DirArray)); if ($Source_Object_TimeStamp > $Dest_Object_TimeStamp) { if (@ftp_put($conn, $Dest_Object, $Source_Object_FullPath , FTP_BINARY) == false) { c_die("Couldn't update file " . $Dest_Object); } echo $Indent . 'Updated file ' . $Dest_Object_FullPath . '
'; flush(); // replace w/clog } } else { // if not on the FTP site then if (ftp_put($conn, $Dest_Object, $Source_Object_FullPath , FTP_BINARY) == false) { c_die("Couldn't add file " . $Dest_Object); } echo $Indent . 'Added file ' . $Dest_Object_FullPath . '
'; flush(); // replace w/clog } } } } // end loop thru source dir } // end IterateThisDir_Source() /******* Recursive loop, pass 3; iterate thru dest dir, clear out stuff that don't exist on source **************/ function IterateThisDir_Dest($conn, $SourceDir, $DestinationDir, $Indent) { // recursive! // Get list of destination directory - stash in $Dest_DirArray $Dest_DirList = ftp_rawlist($conn, $DestinationDir); $Dest_DirArray = ParsedFTPListing($Dest_DirList); // Loop thru the dest directory for ($i=0; $i'; flush(); // replace w/clog } } } // If this is a file, then ... if ($Dest_Object['isdir'] == 0) { // Check for file in the skip list if (FileIsGood($Source_Object)) { if (is_file($Source_Object_FullPath) == false) { // if NOT in the source directory then // delete the file from the dest site if (@ftp_delete($conn, $Dest_Object_FullPath) == false) { c_die("Couldn't delete file " . $Dest_Object_FullPath); } echo $Indent . 'Deleted file ' . $Dest_Object_FullPath . '
'; flush(); // replace w/clog } } } } // end loop thru dest dir } // end IterateThisDir_Dest() /******* Recursive loop, pass 4; iterate thru dest dir, chmod files that need it ********************************/ function IterateThisDir_Chmod($conn, $DestinationDir, $Indent) { // recursive! // Get list of destination directory - stash in $Dest_DirArray $Dest_DirList = ftp_rawlist($conn, $DestinationDir); $Dest_DirArray = ParsedFTPListing($Dest_DirList); // Loop thru the dest directory for ($i=0; $i'; flush(); // replace w/clog } } } // end loop thru dest dir } // end IterateThisDir_Chmod() /**** MAIN ROUTINE **********************************************************************************************/ /** Define your from / to - note this is only when pushing to QC or to Prod Note that we assume "from" is _always_ my local test site, on my local (Win) machine If I am pushing to qc, I am pushing from test to qc If I am pushing to prod, I am pushing from test to prod Note that we assume "to" is always a site that requires FTP, like QC or Prod - on my public server **/ // some global var's you need to set for yourself $SiteName = 'test.mycoolsite.cm'; $SourceStartHere = 'D:/Devel/ApacheRoot/test.mycoolsite.cm'; $DestStartHere = '/www.mycoolsite.cm'; $LocalTempFolder = 'D:/TempWWW'; $ArchiveFolder = 'D:/Devel/Archive'; $TarCommand = 'D:/Programs/Utilities/Unix/tar '; // Open connection to your FTP site $ftp_host = "hostnamehere"; $ftp_user = "usernamehere"; $ftp_pass = "passwordhere"; $myport = 21; $mytimeout = 1; $conn_id = @ftp_connect($ftp_host, $myport, $mytimeout) or c_die("Couldn't connect to " . $ftp_host); echo "Connected to $ftp_host
"; flush(); // replace w/clog if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) { echo "Login successful as $ftp_user@$ftp_host
"; flush(); // replace w/clog } else { c_die("Couldn't login to $ftp_host as $ftp_user"); } // First check for existence of the starting source and destination if (@file_exists($SourceStartHere)) { echo "Initial source directory is " . $SourceStartHere . "
"; flush(); // replace w/clog } else { c_die("Couldn't find source directory " . $SourceStartHere); } if (@ftp_chdir($conn_id, $DestStartHere)) { echo "Initial destination directory is " . ftp_pwd($conn_id) . "

"; flush(); // replace w/clog } else { c_die("Couldn't find initial destination directory " . $DestStartHere); } // Grab local copy of the destination, and before we start changing stuff, we'll tarball it up for recovery echo 'Creating tarball of current content at ' . $DestStartHere . '
'; flush(); // replace w/clog IterateThisDir_ZipDest($conn_id, $LocalTempFolder, $DestStartHere, ''); // tarball filename = DevToTest_sitename_date_time // [Note we put quotes around it, anticipating spaces in the paths] $ArcFileName = '"' . $ArchiveFolder . '/TestToQC_' . $SiteName . '_' . date('Ymd_Hms') . '.tar"'; // We need to give the full path to get the command to run ... $TarParms = '-cf' . $ArcFileName . ' .'; // finally - make the call chdir($LocalTempFolder); system($TarCommand . $TarParms); // too lazy to do this elegantly, so ... system('rd ' . $LocalTempFolder . ' /s /q'); // Iterate through the source directory(ies) - stuff to upload / update echo 'Checking for updates in ' . $SourceStartHere . '
'; flush(); // replace w/clog IterateThisDir_Source($conn_id, $SourceStartHere, $DestStartHere, ''); // Iterate through the destination directory(ies) - stuff to delete from host echo 'Checking for deletions in ' . $DestStartHere . '
'; flush(); // replace w/clog IterateThisDir_Dest($conn_id, $SourceStartHere, $DestStartHere, ''); // Iterate again thru through the destination directory(ies) - flags to chmod echo 'Adjusting file attributes in ' . $DestStartHere . '
'; flush(); // replace w/clog IterateThisDir_Chmod($conn_id, $DestStartHere, ''); // All done ftp_close($conn_id); ?>

All done!