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()])
  • Parameters.
  • $config (array) : Connection values
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

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]])
  • Parameters.
  • $locpath (string) : Local file path
  • $rempath (string) : Remote file path
  • $mode (string) : FTP mode, defaults to ‘auto’ (options are: ‘auto’, ‘binary’, ‘ascii’)
  • $permissions (int) : File permissions (octal)
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

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'])
  • Parameters.
  • $locpath (string) : Local file path
  • $rempath (string) : Remote file path
  • $mode (string) : FTP mode, defaults to ‘auto’ (options are: ‘auto’, ‘binary’, ‘ascii’)
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

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])
  • Parameters.
  • $old_file (string) : Old file name
  • $new_file (string) : New file name
  • $move (bool) : Whether a move is being performed
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

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)
  • Parameters.
  • $old_file (string) : Old file name
  • $new_file (string) : New file name
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

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)
  • Parameters.
  • $filepath (string) : Path to file to delete
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

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)
  • Parameters.
  • $filepath (string) : Path to directory to delete
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

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 = '.'])
  • Parameters.
  • $path (string) : Directory path
  • Returns : An array list of files or FALSE on failure
  • Returns type : Array

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)
  • Parameters.
  • $locpath (string) : Local path
  • $rempath (string) : Remote path
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

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])
  • Parameters.
  • $path (string) : Path to directory to create
  • $permissions (int) : Permissions (octal)
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

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()
  • $permissions (int) : Permissions (octal)
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

EXAMPLE

Here is simple example of close class.

Controller code:-

Your controller code now look like this:-

$this->ftp->close();

Advertisements

Add Comment

📖 Read More