CodeIgniter : Upload file to remote server

Sometimes you need to upload the file to another server. We are going to discuss about how to upload files to remote server in CodeIgniter. CodeIgniter provides FTP class to transfer file to the remote server. Using CodeIgniter’s upload and FTP library you can upload images or files to another server easily. If you are new in CodeIgniter, you can read CodeIgniter Tutorial for Beginners first.

At first we will upload the file to same server using CodeIgniter’s upload library, then transfer file to the remote server using CodeIgniter’s FTP library.


Create a Home controller file called Home.php inside the application/controllers/ directory with Home class. Define a index() function and load the uploading form HTML view. Once upload form is submitted, you should to upload the file to the local server. After that connect to the remote server via FTP and upload file from local server to the remote server. When the remote server uploading is completed, you can delete the uploaded file from the local server. Home controller would look like the below.

('BASEPATH') OR exit('No direct script access allowed');
Home extends CI_Controller
__construct() {
    public function 
//Upload to the local server
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = '*';
//Get uploaded file information
$upload_data $this->upload->data();
$fileName $upload_data['file_name'];
//File path at local server
$source 'uploads/'.$fileName;
//Load codeigniter FTP class
//FTP configuration
$ftp_config['hostname'] = ''
$ftp_config['username'] = 'ftp_username';
$ftp_config['password'] = 'ftp_password';
$ftp_config['debug']    = TRUE;
//Connect to the remote server
//File upload path of remote server
$destination '/assets/'.$fileName;
//Upload file to the remote server
//Close FTP connection
//Delete file from local server


Create home/index.php file into the application/views/ directory and place the uploading form HTML in this file.

<form method="post" enctype="multipart/form-data">
    <label>Choose File</label>
    <input type="file" name="file" />
    <input type="submit" name="submit" value="Upload">

Creating Upload Directory:

 Create upload directory at your local server as per mentioned upload_path into the Home controller.
 Create upload directory at remote server as per mentioned $destination into the Home controller.

Routes Setup:

Open the application/config/routes.php file and change default controller to home.

$route['default_controller'] = 'home';

 All done! Now run project base URL at the browser and upload the file to the remote server.

1 Comment

  1. Samit Sinha Said...

Leave a reply

CodexWorld Newsletter

Subscribe CodexWorld updates via email and get every new post delivered to your inbox.