/** * Builds an array of all files in a directory accompanied * by their lastmodified timestamp. Stores it in the class * variable $snapShot. * * Array Structure: * [i] = 'file' => filename, 'modified' => timestamp * * @param string $path */ function build($path) { $this->snapShot = array(); $localIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::CHILD_FIRST); foreach ($localIterator as $splFileInfo) { if (!$splFileInfo->isDir()) { $filepathAndFilename = $splFileInfo->getPath() . '/' . $splFileInfo->getFilename(); array_push($this->snapShot, array('file' => $filepathAndFilename, 'modified' => getTimeStamp_lastModified($filepathAndFilename))); } } }
function main() { welcomeAscii(); $snapShot = new SnapShot(); $localPath = 'test/test1'; $remotePath = 'testRemote'; $snapShotFileName = 'lastSnapShot.txt'; echo "Loading... "; // If snapshot loads continue, otherwise create a snapshot. if ($snapShot->load($snapShotFileName)) { echo "[32mdone[37m.\n\n"; } else { echo "[32mdone[37m. \n\nDeploying for the first time.\n"; $snapShot->build($localPath); $snapShot->save($snapShotFileName); } echo "Checking for changes...\n"; foreach ($snapShot->files() as $k => $v) { // Remove deleted files from remote directory. if (!file_exists($v['file']) && file_exists($remotePath . '/' . $v['file'])) { echo "Removing " . $v['file'] . " from remote directory...\n"; unlink($remotePath . '/' . $v['file']); continue; } // Copy new files to remote directory if (!file_exists($remotePath . '/' . $v['file'])) { if (!is_dir($remotePath . '/' . dirname($v['file']))) { mkdir($remotePath . '/' . dirname($v['file']), intval(fileperms($v['file'])), true); } echo "Copying new file " . $v['file'] . " to " . $remotePath . "/" . basename($v['file']) . "\n"; if (!copy($v['file'], $remotePath . '/' . $v['file'])) { echo "Failed to copy new file " . $v['file'] . " to remote location...\n"; } continue; } // Copy modified files to remote directory if (getTimeStamp_lastModified($v['file']) > getTimeStamp_lastModified($remotePath . '/' . $v['file'])) { echo "Updating " . $localPath . $v['file'] . " to " . $remotePath . '/' . $v['file'] . "\n"; if (!copy($v['file'], $remotePath . '/' . $v['file'])) { echo "Failed to copy file to remote location: " . $v['file'] . "...\n"; } } } // Update SnapShot $snapShot->build($localPath); $snapShot->save($snapShotFileName); echo "\n[32mAll files in sync.[37m\n\n"; }