Forum Moderators: coopster

Message Too Old, No Replies

Copy Recursive Directories in PHP

Move Srom Staging Server to Live Site

         

jbalestrieri

12:33 am on Feb 8, 2004 (gmt 0)

10+ Year Member



Ok, I'm banging my head and am wondering if this is even possible.

I need to be able to do a recursive copy of a directory on a staging server (virtual) to a the production server (another virtual on the same machine.) I've been tring the examples at [php.net...] after much tweaking, I still can not get this to work. I get no errors anywhere and when I check the paths they are correct. The permissions are correct as well.

My Question: Is this even possible?

Here is the code I've been trying...
@extract($_POST);

$basedir = "/var/www/html/internal/contents/projects/";
$tobase ="/var/www/html/live/contents/projects/";
$wf = "$basedir$type";
$wto = "$tobase$type";

echo "$wf<br>";
echo "$wto<br>";

function cp($wf, $wto){ // it moves $wf to $wto
if (!file_exists($wto)){//the improvement
mkdir($wto,0777) or die ("Could not chmod destination (this is line 21");
}
$arr=ls_a($wf);
foreach ($arr as $fn){
if($fn){
$fl="$wf/$fn";
$flto="$wto/$fn";
if(is_dir($fl)) cp($fl,$flto);
else copy($fl,$flto) or die ("could not copy directory ");
}
}
}

function ls_a($wh){
if ($handle = opendir($wh)) {
while (false!== ($file = readdir($handle))) {
if ($file!= "." && $file!= ".." ) {
if(!$files) $files="$file";
else $files="$file\n$files";

}
}
closedir($handle);
}
$arr=explode("\n",$files);
for($i=0;$i<count($arr);$i++){
echo "<br>$arr[$i]";
}
return $arr;
}
?>

coopster

5:48 pm on Feb 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What is (or isn't) happening? I don't see a call to your cp() function. Are you invoking it anywhere? For instance:

echo "$wf<br>";
echo "$wto<br>";
if cp($wf, $wto) print "All done!";
function cp($wf, $wto){ // it moves $wf to $wto

dcrombie

11:36 am on Feb 11, 2004 (gmt 0)



Why don't you a command-line tool like rsync. So much easier ;)

jbalestrieri

12:37 pm on Feb 11, 2004 (gmt 0)

10+ Year Member



I did end up going with resync. Thanks everyone.

coopster

12:53 pm on Feb 11, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Cool. rsync is the same tool used by PHP to mirror their site [php.net].

If this is a public server please take note that there is a security advisory for rsync versions prior to 2.5.7.

** If you're using a version prior to 2.5.7, please see the security advisory below! **

h**p://rsync.samba.org/#security