Part of the EllisLab Network
   
 
Improved FTP Class
Posted: 29 July 2007 12:30 PM   [ Ignore ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3150
Joined  06-11-2007

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

<?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.codeigniter.com/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:

$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.

// 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.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 29 July 2007 01:10 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  393
Joined  06-23-2006

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.

class MY_FTP extends CI_FTP {

and then put all of your modifications into MY_FTP class.

 Signature 

Mac OS X 10.7, Apache 2.x, NGiNX, PHP 5.3.x, CodeIgniter 1.7.2., baby!

Profile
 
 
Posted: 30 July 2007 05:40 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3150
Joined  06-11-2007

Indeed! Updated origional post to show the extention instead, with a bugfix.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 04 August 2007 03:17 PM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3150
Joined  06-11-2007

Updated the link to a new version which contains FTP Download functionality.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
Posted: 10 October 2008 06:57 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  26
Joined  07-08-2006

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.

Profile
 
 
Posted: 08 January 2009 09:06 AM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3150
Joined  06-11-2007

Ignore this post, replied to the wrong FTP related thread.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

Profile
 
 
   
 
 
‹‹ Paypal_Lib implementation      SORTING ››