Welcome Guest, Not a member yet? Register   Sign In
Improved FTP Class
#1

[eluser]Phil Sturgeon[/eluser]
I recently made a class takes config "groups" just like the database config. I used this as I wanted to be able to connect to various different servers within the same file without having to enter the config arrays many times.

/application/config/Ftp.php

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| FTP
| -------------------------------------------------------------------
| This file contains connection details for your sites FTP server.
| This is used for uploading of large files (over 2 megs) to stop
| the timing out of upload sessions.
|
| Please see user guide for more info:
| http://www.ellislab.com/codeigniter/user-guide/libraries/ftp.html
|
*/

$active_group = "default";

$ftp['default']['hostname'] = 'somesite.com';
$ftp['default']['username'] = 'bobby';
$ftp['default']['password'] = 'password';
$ftp['default']['port']     = 21;
$ftp['default']['passive']  = FALSE;
$ftp['default']['debug']    = TRUE;
$ftp['default']['ssl_mode'] = FALSE;

$ftp['media_server']['hostname'] = 'otherhost.com';
$ftp['media_server']['username'] = 'mcdoogle';
$ftp['media_server']['password'] = 'secret';
$ftp['media_server']['port']     = 21;
$ftp['media_server']['passive']  = FALSE;
$ftp['media_server']['debug']    = TRUE;
$ftp['media_server']['ssl_mode'] = FALSE;


?>

/application/libraries/MY_Ftp.php
Code on Pastebin for now

The benefits of this are that it can be used EXACTLY like the old FTP library, with some added usefulness with the multiple connection types, and obviously saves on repeated code. You can pass it custom config variables, or simply the name of one of your groups like so:

Code:
$this->ftp->connect('media_server');                        $this->ftp->move('public_html/local/file.png', 'public_html/remote/file.png');
$this->ftp->close();

It also supports FTP SSL connections. For error messages to be able to show, you will need to add the line below to your ftp lang file.

Code:
// Custom
$lang['ftp_ssl_not_supported']        = "Unable to connect to the server using SSL.";

Please test it and let me know how it goes. If it works ok then i'll put it on the Wiki.
#2

[eluser]coolfactor[/eluser]
Thanks for your contributions.

However, if you're going to "improve" a built-in class, it is best to extend the class rather than replace it outright.

Code:
class MY_FTP extends CI_FTP {

}

and then put all of your modifications into MY_FTP class.
#3

[eluser]Phil Sturgeon[/eluser]
Indeed! Updated origional post to show the extention instead, with a bugfix.
#4

[eluser]Phil Sturgeon[/eluser]
Updated the link to a new version which contains FTP Download functionality.
#5

[eluser]eger[/eluser]
I wanted to say thanks for the download function in this extended class. I think the original CI FTP class should include the download function. I was puzzled to find no way to download files. I was able to extend the original class and add your download function and it works just fine.
#6

[eluser]Phil Sturgeon[/eluser]
Ignore this post, replied to the wrong FTP related thread.




Theme © iAndrew 2016 - Forum software by © MyBB