I'd appreciate any advice on how to make this script more efficient. I run it from the shell to archive an unlimited # of subdirs to their own seperate .tar files.
#!/usr/bin/php -qC
<?php
if (!($argc == 3 || in_array($argv[1], array('--help', '-help', '-h', '-?')))) {
echo "\n"
."########################\n"
."# PHP Project Archiver #\n"
."########################\n"
."\n";
echo "Usage:\n";
echo basename($argv[0])." <projectDir> <destination>\n\n";
echo "<projectDir> is the path of the project directory to archive. (ie. /projects2/someproject)\n"
."<destination> is the path where the tar files will be saved.\n\n"
." NOTE: no trailing slashes!\n\n";
} else {
archiveDir($argv[1],$argv[2]);
}
//begin functions
function archiveDir($pdir,$dest) {
chdir($pdir);
shell_exec("find ./ -type d > dirstruct.txt");
$whichTo = explode("\n",fget_contents("dirstruct.txt"));
// Loop through our array.
foreach ($whichTo as $toArc) {
$isEmpty = shell_exec("find $toArc -type f -maxdepth 1");
if (($toArc!="") && ($isEmpty!="")) {
echo "Archiving $toArc\n";
$pname = basename($pdir);
$fname = basename($toArc);
if (($fname!="") && ($fname!=".")) {
shell_exec("tar -cf $dest/$pname.$fname.tar `find $toArc -type f -maxdepth 1`");
} else {
shell_exec("tar -cf $dest/$pname.rootdir.tar `find $toArc -type f -maxdepth 1` --exclude dirstruct.txt");
}
}
}
echo "\nAll done!\n\n";
}
function fget_contents($f) {
ob_start();
$retval = @readfile($f);
if (false !== $retval) { // no readfile error
$retval = ob_get_contents();
}
ob_end_clean();
return $retval;
}
?>