How to do server to server ftp transfer in PHP

It is quite difficult to transfer web site from one server to another. Here is a solution for server to server ftp transfer.

  • Zip the required files as one file. The zipping feature is usually available in your web hosting control panel’s file manager.
  • Upload the below mentioned script on the source server.
  • Run the script on the source server.
  • This will transfer files from one server to the other.
  • Unzip the files on the destination server.
This script can be used for other purposes as well.

<?php
$server = “ftp.you-server.com”; //address of ftp server (leave out ftp://)
$ftp_user_name = “userName”; // Username
$ftp_user_pass = “passwordHere”; // Password
$source = “/home/folder/public_html/filename.ext”;
$dest = “/public_html/filename.ext”;
$mode=”FTP_BINARY”;
$connection = ftp_connect($server);

$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);

if (!$connection || !$login) { die(‘Connection attempt failed!’); }

$upload = ftp_put($connection, $dest, $source, FTP_BINARY);

if (!$upload) { echo ‘FTP upload failed!’; }

ftp_close($connection);
echo “done”;
?>

One thought on “How to do server to server ftp transfer in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *


eight − = 7

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>