Home > Aubrey Falconer > Content > Bug Reports > : Mirroring a website over FTP
Mirroring a website over FTP
0 Comments - 9778 Views
Need to mirror your website to a different host? It's easy with PHP!
Submitted By admin on 06/01/25
Aubrey Falconer, php, coding 
This originally posted in the "Aubrey Falconer" Group

Recently I needed a way to mirror my entire website's files to a different webhost.
Here is what I did:



Step 1:

Upload a php file to your current server containing the below code:
(remember to change the appropriate variables)

<?php
set_time_limit(9999);

//**************** Change between these lines ****************

$remote_path = "/public_html/"; // The folder you will be copying into on the remote ftp site
$local_path = '/home/username/public_html/'; // Your sites directory tree leading to the folder you will be copying from

$ftpc = ftp_connect(gethostbyname("ftp.mydomain.com")); // update with the address of the ftp server
$ftpr = ftp_login($ftpc,"username","password"); // Enter the ftp username and password
//ftp_pasv ( $ftpc, true ); // uncomment to use passive ftp mode

//**************** End change between these lines ****************

ftpRec();
ftp_close($ftpc);

function ftpRec ($srcrela = '') {
global $local_path;
global $remote_path;
global $ftpc;
global $ftpr;

chdir($local_path.$srcrela);

if (!ftp_chdir($ftpc,$remote_path.$srcrela)) {
ftp_mkdir ($ftpc,$remote_path.$srcrela);
ftp_chdir ($ftpc,$remote_path.$srcrela);
}

if ($handle = opendir(".")) {
while (false !== ($fil = readdir($handle))) {
if ($fil != "." && $fil != "..") {
if (!is_dir($fil)) {
ftp_put($ftpc, $remote_path.$srcrela.$fil, $fil, FTP_BINARY);
}
else {
ftpRec ($srcrela.$fil."/");
chdir ("../");
}
}
}
closedir($handle);
}
}
?>



Step 2:

Visit the file you just uploaded in your browser (something like http://yourdomain.com/mirror.php).
This will run the file, which should automatically mirror your website to the new ftp server!
Always remember to clean up after yourself - make sure you delete the mirror.php file when you are done using it.

How it works:

The script loops through every file of your site, copying it over to the new server as it goes.

Credits:

The script was designed after reading through all of the comments on php.net regarding php's ftp functions.
The author wishes to thank all the people who contributed their comments, as well as the developers of php who make this all possible!

No Comments Yet
Post a Comment

GenTime: 0.01 seconds

Site Design and Graphics Copyright 2002 - 2020 by Aubrey
Use of this site constitutes agreement to our » Legal Stuff