How to transfer website files between web servers using PHP and FTP

How to transfer website files between web servers using PHP and FTP

Moving website files between servers is easy and quick if you use a PHP script to FTP the files from the source server to the destination server. Downloading the website to your own computer, and then re-uploading the files to the destination web server is slow and unnecessary. Here is a PHP FTP script that helps...

I am about to transfer a Drupal installation from one PHP web server to another. It is a multisite Drupal install, with a lot of support files. In the past I have downloaded the entire file structure to my computer, and then uploaded the entire file structure to the new web server. This takes a long time. I looked around for a PHP script to make this quicker, and a great example script at uk.php.net/ftp .

I have modified the script slightly and reposted it here for convenience. I am not taking credit for any work here, the credit lies with Kristy Christie the original poster.

<?php

// --------------------------------------------------------------------
// THE TRIGGER
// --------------------------------------------------------------------

// set the various variables
$ftproot = "/public_html/folder/";
$srcroot = "/home/username/public_html/";
$srcrela = "folder/";

// connect to the destination FTP & enter appropriate directories both locally and remotely
$ftpc = ftp_connect("ftp.yourdomain.com");
$ftpr = ftp_login($ftpc,"username","password");

if ((!$ftpc) || (!$ftpr)) { echo "FTP connection not established!"; die(); }
if (!chdir($srcroot)) { echo "Could not enter local source root directory."; die(); }
if (!ftp_chdir($ftpc,$ftproot)) { echo "Could not enter FTP root directory."; die(); }

// start ftp'ing over the directory recursively
ftpRec ($srcrela);

// close the FTP connection
ftp_close($ftpc);

// --------------------------------------------------------------------
// THE ACTUAL FUNCTION
// --------------------------------------------------------------------
function ftpRec ($srcrela)
{
global $srcroot;
global $ftproot;
global $ftpc;
global $ftpr;

// enter the local directory to be recursed through
chdir($srcroot.$srcrela);

// check if the directory exists & change to it on the destination
if (!ftp_chdir($ftpc,$ftproot.$srcrela))
{
// remote directory doesn't exist so create & enter it
ftp_mkdir ($ftpc,$ftproot.$srcrela);
ftp_chdir ($ftpc,$ftproot.$srcrela);
}

if ($handle = opendir("."))
{
while (false !== ($fil = readdir($handle)))
{
if ($fil != "." && $fil != "..")
{
// check if it's a file or directory
if (!is_dir($fil))
{
// it's a file so upload it
ftp_put($ftpc, $ftproot.$srcrela.$fil, $fil, FTP_BINARY);
}
else
{
// it's a directory so recurse through it
/* if ($fil == "templates")
if ($fil == "templates")
{
// I want the script to ignore any directories named "templates"
// and therefore, not recurse through them and upload their contents
}
else*/
{
ftpRec ($srcrela.$fil."/");
chdir ("../");
}
}
}
}
closedir($handle);
}
}
?>

Post new comment
The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <img>
  • Lines and paragraphs break automatically.

More information about formatting options