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);
?>
10 Comments
Sorry, the comment form is closed at this time.


20:12 on August 2nd, 2009
Hmm… I read blogs on a similar topic, but i never visited your blog. I added it to favorites and i’ll be your constant reader.
23:02 on August 3rd, 2009
Are you a professional journalist? You write very well.
13:59 on August 31st, 2009
Its giving error, Pls help:
Warning: opendir(/var/www/vhosts/mysite.com/httpdocs/images/) [function.opendir]: failed to open dir: No such file or directory in D:Latest_projectscopydir.php on line 19
$source_directory ="/var/www/vhosts/mysite.com/httpdocs/images/";
open_dir function is giving error, while the images folder is there.
Testing from offline to my online site.
14:09 on September 1st, 2009
I think you are using windows, you have set source_directory like "d:\\apache\\root\\site.com\\" Where is your files running?
08:53 on October 1st, 2009
Try \ for directory
Ex:
c:\xampp\htdocs\Flipbook\dyna_flipincludesfunctions.php
08:55 on October 1st, 2009
Are you sure that directory is true?
08:56 on October 1st, 2009
Try \\\\ for directory Ex: c:\\\\xampp\\\\htdocs\\\\Flipbook\\\\dyna_flipincludesfunctions.php
08:56 on October 1st, 2009
Try double for typing directory
10:03 on October 1st, 2009
What was the problem? Could you tell me for dont do that problem again
10:23 on October 1st, 2009
Thanks for share