Skip to content

Uploading file via FTP using CodeIgniter

Last updated on May 6, 2018

We are going to discuss how to upload files to the remote server in CodeIgniter using CI’s FTP library. This library has limitations – SFTP and SSL FTP protocols are not supported, only standard FTP.

CodeIgniter’s FTP Class permits files to be transferred to a remote server. Remote files can also be moved, renamed, and deleted. The FTP class also includes a “mirroring” function that permits an entire local directory to be recreated remotely via FTP.

Uploading file via FTP using CodeIgniter

Let’s start the tutorial, first, we will upload the file to the same server using CodeIgniter’s upload library, then we are going to transfer the file to the remote server using CodeIgniter’s FTP library.

Create upload directory in the local server to store uploaded files or images, we are going to mention this directory name in the CI’s upload library configuration.

Create destination directory in the remote server to store uploaded files or images, we are going to mention this directory name while configuring the CI’s FTP library.

Controller:
Create a controller file called Upload.php inside the application/controllers/ directory. Define a index() function and load the HTML view( upload form ).

Once upload form is submitted, we will upload the file to the local server and after that, we are connecting to the remote server via FTP and uploading files to the remote server from the local server. When the remote server uploading is completed, you are deleting the uploaded file from the local server.

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('upload_view');
    }
}

View:
Create a file called upload_view.php in application/views and place the uploading form HTML in this file.

5 2 votes
Article Rating
Subscribe
Notify of
guest

4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments