CodeIgniter Forums

Full Version: FTP Class Enhancement (my first little contribution)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
Hi there,

I'm using CodeIgniter to create a little CMS, and I used the FTP Library to do many files-related operations, without using the classic filesystem methods (I really don't like all the permission-related stuff and problems).

Here's the point: I've noticed that the library missed a file_copy and directory_copy methods. So I wrote something by myself. Also, I added a new config var called "base_folder".

The var $base_folder represents the path to the main application directory, starting from base folder of the current ftp user.

An example:

- the start folder will be "/";
- our application folder is "httpdocs/myapplication/"

so, $base_folder will be "httpdocs/myapplication/".

Here's an example of the "application/config/ftp.php" file with the new $base_folder var:

Code:
<?php

$config['hostname'] = '127.0.0.1';
$config['username'] = 'ftpuser';
$config['password'] = 'ftppassword';
$config['port']     = 21;
$config['passive']  = TRUE;
$config['debug']  = TRUE;
$config['base_folder']  = 'my/path/to/application';

?>

Now, here's the code. Here I add the $base_folder:

Code:
class CI_FTP {

var $hostname = '';
var $username = '';
var $password = '';
var $port = 21;
var $passive = TRUE;
var $debug = FALSE;
var $conn_id = FALSE;

        // here's the variable! the default value is an empty string
        var $base_folder = '';

After that, here's the file_copy code:

Code:
/**
  * Reads a file and copies it on another place on the same server.
  *
  * This function takes two parameters ($source and $destination) and
         * uses ftp_fput to copy a file on the same server.
  *
  * @access public
  * @param string path to source
  * @param string path to destination
  * @return bool
  */
        function file_copy($source, $destination)
        {
            if (!$this->_is_conn()) return FALSE;
            
            $destination = $this->base_folder.$destination;
            
            if(ftp_fput($this->conn_id, $destination, fopen($source, 'r'), FTP_BINARY))
                return TRUE;
            else
            {
                if ($this->debug == TRUE)
                {
                    $this->_error('ftp_unable_to_copy');
                }
            }
        }

I used the "ftp_fput" php standard method. Also, if there are some problems, I call the _error method giving as argument "ftp_unable_to_copy". As you can easily imagine, that's not a standard string, so you'll have to update the "system/language/english/ftp_lang.php":

Code:
<?php

$lang['ftp_no_connection']  = "Unable to locate a valid connection ID. Please make sure you are connected before peforming any file routines.";
$lang['ftp_unable_to_connect']  = "Unable to connect to your FTP server using the supplied hostname.";
$lang['ftp_unable_to_login']  = "Unable to login to your FTP server. Please check your username and password.";
$lang['ftp_unable_to_makdir']  = "Unable to create the directory you have specified.";
$lang['ftp_unable_to_changedir'] = "Unable to change directories.";
$lang['ftp_unable_to_chmod']  = "Unable to set file permissions. Please check your path. Note: This feature is only available in PHP 5 or higher.";
$lang['ftp_unable_to_upload']  = "Unable to upload the specified file. Please check your path.";
$lang['ftp_unable_to_download']  = "Unable to download the specified file. Please check your path.";
$lang['ftp_no_source_file']  = "Unable to locate the source file. Please check your path.";
$lang['ftp_unable_to_rename']  = "Unable to rename the file.";
$lang['ftp_unable_to_delete']  = "Unable to delete the file.";
$lang['ftp_unable_to_move']  = "Unable to move the file. Please make sure the destination directory exists.";

// here's the extra error string!
$lang['ftp_unable_to_copy']             = "Unable to copy file. Please check both destination and source paths.";

Then, the last method: "directory_copy". Here it is:

Code:
/**
  * Reads a directory, then copies its content on another folder on the
         * same server.
  *
  * This function takes two parameters ($source and $destination) and
         * uses ftp->mkdir() and file_copy() to copy a file on the same server.
         * If there are other subfolders, the function call itself recursively.
  *
  * @access public
  * @param string path to source
  * @param string path to destination
  * @return bool
  */
        function directory_copy($source, $destination)
        {
            if (!$this->_is_conn()) return FALSE;

            $directory = dir($source);

            while ($file = $directory->read())
            {
                if ($file != "." && $file != "..")
                {
                    if (is_dir($source."/".$file))
                    {
                        $this->mkdir($destination."/".$file);
                        
                        $this->directory_copy($source."/".$file, $destination."/".$file);  
                    }
                    else
                        $this->file_copy($source."/".$file, $destination."/".$file);
                }
            }

            $directory->close();
        }

This is a recursive method, so it can copy every kind of folder structure. To recreate the start structure, I used the "mkdir" and "file_copy" methods.

Using them is very easy: here's an example.

Code:
class Welcome extends CI_Controller {

public function index()
{
            $this->load->library('ftp');
            
            if($this->ftp->connect())
            {
                // example call
                $this->ftp->file_copy('folder1/startfile.php', 'folder1/endfile.php');
                
                // example call
                $this->ftp->directory_copy('start_folder', 'end_folder');
            }
}
}

Hope everything is clear! Sorry for my english, I think I'm not very good. However, hope it will be useful for someone Smile