Copy a Directory to Another Server
31
Jul10
Jul10
I need a script to my directory to another servers. Firstly, i tried to download from my site and upload another site but it is not so fast. Because there is lots of files in that directory. I wrote a little php script for this. I hope you will enjoy..
P.S.: This code is only copy files in that directory, if there is another directories this script didnt copy them..
<?
$ftp_server = www.mysite.com;
$ftp_username = "myuser";
$ftp_password = "mypass";
$source_directory = "/home/mywebsite/public_html/test/";
$destination_directory = "public_html/test/";
$ftp_connection = ftp_connect($ftp_server);
$connection_result = ftp_login($ftp_connection, $ftp_username, $ftp_password);
if ((!$ftp_connection) || (!$connection_result)) {
echo("<font color=red>Connection Error!...</font><br>");
echo("<font color=red>$ftp_username user can't connect to $ftp_server ...</font><br>");
exit;
} else {
echo("<font color=green>$ftp_username connected to $ftp_server ...</br>");
}
$copied_directory = opendir($source_directory);
while($my_file = readdir($copied_directory))
{
if($my_file != "." && $my_file != "..")
{
$source_my_file = $source_directory.$my_file;
$target_my_file = $destination_directory.$my_file;
$upload = ftp_put($ftp_connection, $target_my_file, $source_my_file, FTP_BINARY);
if (!$upload)
{
echo "<font color=red>Can't connect...</font>";
}
else
{
echo "<font color=green> $my_file is copied to $ftp_server ...</font><br>";
}
}
}
ftp_close($ftp_connection);
?>

