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.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller
{
function __construct() {
parent::__construct();
}
public function index(){
if($this->input->post('submit')){
//Upload to the local server
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
if($this->upload->do_upload('file'))
{
//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
$this->load->library('ftp');
//FTP configuration
$ftp_config['hostname'] = 'ftp.example.com';
$ftp_config['username'] = 'ftp_username';
$ftp_config['password'] = 'ftp_password';
$ftp_config['debug'] = TRUE;
//Connect to the remote server
$this->ftp->connect($ftp_config);
//File upload path of remote server
$destination = '/assets/'.$fileName;
//Upload file to the remote server
$this->ftp->upload($source, ".".$destination);
//Close FTP connection
$this->ftp->close();
//Delete file from local server
@unlink($source);
}
}
$this->load->view('home/index');
}
}
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"> </form>
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.
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.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
Nice article, thanks you for shearing,
Thanks
Great !! Though uploading to another host is not generally required but the upload process as instructed here is awesome.