Friday, June 06, 2014

FTP File transfer using PHP

Couple of times I have faced situations where I need to backUp all files on a server and shift to another. This becomes tedious if it is done through normal FTP clients like FileZilla or CuteFTP etc because file downloading to your local machine and uploading to the target server may take several hours. Or, if we try to download and upload via CPanel, it will take same amount time and there will be additional risk of breaking of download/upload if network connection is lost for any reason.

To avoid this huge time wastage, we can do the following things ::

1. Use CPanel to ZIP all the source files in Source server
2. Then Upload a small PHP file to Source server and run it
3. This PHP file uploads the big backup ZIP archive to the Target server through FTP
4. On the Target server, we unzip the ZIP archive 

What we would require ::

1. CPanel access of both the source and target servers
2. FTP access of target server

So, the process is pretty simple, the important part is to write the PHP file which does the main job. 

Check the PHP code below.

First we use ftp_connect() function to connect to Target server, if this connection is not established, we don't proceed further.

Secondly, use use ftp_put() function to upload the file to Target from Source server.


<?php
/// FTP Uploader Application

/// SET FTP Login details for Target Server
$ftp_server = "targetserver.com"; 
$ftp_user_name = "ftpUserName";
$ftp_user_pass = "ftpPassword";

// set up the FTP connection
$conn_id = ftp_connect($ftp_server) or die("Could not FTP Connect to $ftp_server"); 

// login with FTP username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// check connection
if ((!$conn_id) || (!$login_result)) 

   echo "<br><span style='color:red'>FTP connection has failed!</span>";
   exit; 

else 
{
   echo "<br>Connected to <b>$ftp_server</b>, for user <b>$ftp_user_name</b>";
}

// GET the file, which needs to be transferred
$source_file = "public_html.tar.gz";

// Set the TARGET location
$destination_file = "public_html/public_html.tar.gz";

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) 

   echo "<br><span style='color:red'>FTP upload has failed!</span>";

else 
{
   echo "<br>Uploaded [$source_file] successfully to <span style='color:green'>$ftp_server</span> as [$destination_file]";
}

// close the FTP stream 
ftp_close($conn_id); 
?>

The above code is quite self-explanatory. 

There are some other FTP functions like as shown below :;

Connection Related ::
ftp_ssl_connect() :- For making Secure SSL-FTP connection
ftp_pasv()  :- Set PASSIVE mode to ON/OFF

File/Folder Edit/Delete/Rename ::
ftp_get() :- Download a File from the Target Server 
ftp_mkdir()  :- Create new folder on Target server
ftp_rmdir()  :- Remove folder from Target server
ftp_rename()  :- We can rename old folders/files on the Target server 
ftp_delete()  :- Delete a file from the Target server

Browse the Target Server :
ftp_chdir() :- Change directory
ftp_pwd() :- Get current directory in the Target server
ftp_nlist() :- Returns a list of files in the given directory

Some examples ::
ftp_chdir($conn_id,"public_html");
ftp_get($conn_id,'Ro_bo_ts.TxT','robots.txt',FTP_ASCII);

The above 2 lines, changes current directory to 'public_html' and downloads a file 'robots.txt' in ASCII mode and saves it as 'Ro_bo_ts.TxT' on local directory.

print_r(ftp_nlist($conn_id,'public_html'));
The above line Get listing of all the folders/files inside 'public_html' folder.