Tutorialsplane

Codeigniter FTP Class Library


Codeigniter FTP Class Library – We can load codeigniter ftp class library like this $this->load->library(‘ftp’);. This library provide permits you to be transfered file to remote server. Remote file can be move , rename and delete. This library permit entire local directory to be recreated remotely with FTP class. Here in this tutorial, we are going to explain how to use FTP class library.


Codeigniter FTP class library

Let us understand how FTP class library works in codeigniter with examples.

Load FTP class library

First load FTP class library to use its functions, this library can be loaded simply as below-

How to load FTP class library:

$this->load->library('ftp');

Class reference:-

There are following references available in ftp class library. Now we will explain one by one.

1. Connect.

This function is used to Connects and logs to the FTP server.

connect([$config = array()])

EXAMPLE

Here is simple example of connect class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ftp_controller extends CI_Controller 
{
public function index()
{
$this-?>load->library('ftp');
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['debug']    = TRUE;
$this->ftp->connect($config);
}}
?>

2. Uploads.

This is used to uploads a file to your server.

upload($locpath, $rempath[$mode = 'auto'[$permissions = NULL]])

EXAMPLE

Here is simple example of upload class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ftp_controller extends CI_Controller 
{
public function index()
{
$this-?>load->library('ftp');
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['debug']    = TRUE;
$this->ftp->connect($config);
$this->ftp->upload('local path to folder', 'myfile.html', 'ascii', 0775);
$this->ftp->close();
}}
?>

3. Download.

This is used to download a file from your server.

download($rempath, $locpath[$mode = 'auto'])

EXAMPLE

Here is simple example of download class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ftp_controller extends CI_Controller 
{
public function index()
{
$this-?>load->library('ftp');
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['debug']    = TRUE;
$this->ftp->connect($config);
$this->ftp->download('myfile.html', 'local path to folder', 'ascii');
$this->ftp->close();
}}
?>

4. Rename.

This method permits you to rename your file.

rename($old_file, $new_file[$move = FALSE])

EXAMPLE

Here is simple example of rename class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ftp_controller extends CI_Controller 
{
public function index()
{
$this-?>load->library('ftp');
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['debug']    = TRUE;
$this->ftp->connect($config);
$this->ftp->rename('myfile.html', 'file.html');
$this->ftp->close();
}}
?>

5. Move.

Move class is used to move a file.

move($old_file, $new_file)

EXAMPLE

Here is simple example of move class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ftp_controller extends CI_Controller 
{
public function index()
{
$this-?>load->library('ftp');
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['debug']    = TRUE;
$this->ftp->connect($config);
$this->ftp->move('old path', 'new path');
$this->ftp->close();
}}
?>

6. Delete file.

Delete method is used to delete a file.

delete_file($filepath)

EXAMPLE

Here is simple example of delete file class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ftp_controller extends CI_Controller 
{
public function index()
{
$this-?>load->library('ftp');
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['debug']    = TRUE;
$this->ftp->connect($config);
$this->ftp->delete_file('filepath');
$this->ftp->close();
}}
?>

7. Delete directory.

This method is used to delete a directory.

delete_dir($filepath)

EXAMPLE

Here is simple example of delete directory class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ftp_controller extends CI_Controller 
{
public function index()
{
$this-?>load->library('ftp');
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['debug']    = TRUE;
$this->ftp->connect($config);
$this->ftp->delete_dir('path directory');
$this->ftp->close();
}}
?>

8. List files.

This function gives permit to retrieve a list of file on your server return as an array.

list_files([$path = '.'])

EXAMPLE

Here is simple example of list files class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ftp_controller extends CI_Controller 
{
public function index()
{
$this-?>load->library('ftp');
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['debug']        = TRUE;
$this->ftp->connect($config);
$list = $this->ftp->list_files('filepath to folder');
print_r($list);
$this->ftp->close();
}}
?>

9. Mirror.

Mirror method is used to recreated the orignal path on the server.

mirror($locpath, $rempath)

EXAMPLE

Here is simple example of mirror class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ftp_controller extends CI_Controller 
{
public function index()
{
$this-?>load->library('ftp');
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['debug']        = TRUE;
$this->ftp->connect($config);
$this->ftp->mirror('orignal_path', 'recreated_path');
$this->ftp->close();
}}
?>

10. Mkdir.

This method is set octal value in second parameter.

mkdir($path[$permissions = NULL])

EXAMPLE

Here is simple example of mkdir class.

Controller code:-

Your controller code now look like this:-

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ftp_controller extends CI_Controller 
{
public function index()
{
$this-?>load->library('ftp');
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';
$config['debug']        = TRUE;
$this->ftp->connect($config);
$this->ftp->mkdir('directory_path', 0755);
$this->ftp->close();
}}
?>

11. Close.

This function is used to close connection to the server.

close()

EXAMPLE

Here is simple example of close class.

Controller code:-

Your controller code now look like this:-

$this->ftp->close();

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference