Guest

FTP Class w Download

By: a guest on Aug 4th, 2007  |  syntax: None  |  size: 3.61 KB  |  hits: 655  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 4.3.2 or newer
  6.  *
  7.  * @package             CodeIgniter
  8.  * @author              Rick Ellis
  9.  * @copyright   Copyright (c) 2006, EllisLab, Inc.
  10.  * @license             http://www.codeignitor.com/user_guide/license.html
  11.  * @link                http://www.codeigniter.com
  12.  * @since               Version 1.0
  13.  * @filesource
  14.  */
  15.  
  16. // ------------------------------------------------------------------------
  17.  
  18. /**
  19.  * FTP Class
  20.  *
  21.  * @package             CodeIgniter
  22.  * @subpackage  Libraries
  23.  * @category    Libraries
  24.  * @author              Rick Ellis ( Modified by Philip Sturgeon )
  25.  * @link                http://www.codeigniter.com/user_guide/libraries/ftp.html
  26.  */
  27. class MY_FTP extends CI_FTP {
  28.  
  29.         var $ssl_mode   = FALSE;
  30.        
  31.         /**
  32.          * Constructor - Sets Preferences
  33.          *
  34.          * The constructor can be passed an array of config values
  35.          */    
  36.         function MY_FTP($config = array())
  37.         {              
  38.                 if (count($config) > 0)
  39.                 {
  40.                         $this->initialize($config);
  41.                 }      
  42.  
  43.                 log_message('debug', "FTP Class Initialized");
  44.         }
  45.  
  46.         // --------------------------------------------------------------------
  47.  
  48.         /**
  49.          * Initialize preferences
  50.          *
  51.          * @access      public
  52.          * @param       array
  53.          * @return      void
  54.          */    
  55.         function initialize($config = array())
  56.         {
  57.                 foreach ($config as $key => $val)
  58.                 {
  59.                         if (isset($this->$key))
  60.                         {
  61.                                 $this->$key = $val;
  62.                         }
  63.                 }
  64.                
  65.                 // Prep the hostname
  66.                 $this->hostname = preg_replace('|.+?://|', '', $this->hostname);
  67.         }
  68.  
  69.         // --------------------------------------------------------------------
  70.  
  71.         /**
  72.          * FTP Connect
  73.          *
  74.          * @access      public
  75.          * @param       array    the connection values
  76.          * @return      bool
  77.          */    
  78.         function connect($config = '')
  79.         {              
  80.                 // Load the DB config file if a DSN string wasn't passed
  81.                 if (is_string($config))
  82.                 {
  83.                         include(APPPATH.'config/Ftp'.EXT);
  84.                        
  85.                         $group = ($config == '') ? $active_group : $config;
  86.                        
  87.                         if ( ! isset($ftp[$group]) || !is_array($ftp[$group]))
  88.                         {
  89.                                 show_error('You have specified an invalid ftp connection group: '.$group);
  90.                         }
  91.                         else
  92.                         {
  93.                                 $this->initialize($ftp[$group]);
  94.                         }
  95.                 }
  96.                
  97.                 elseif(is_array($config))
  98.                 {
  99.                         if (count($config) > 0)
  100.                         {
  101.                                 $this->initialize($config);
  102.                         }      
  103.                 }
  104.                
  105.                 if($this->ssl_mode == TRUE)
  106.                 {
  107.                         if(function_exists('ftp_ssl_connect'))
  108.                         {
  109.                                 $this->conn_id = @ftp_ssl_connect($this->hostname, $this->port);
  110.                         }
  111.                         else
  112.                         {
  113.                                 $this->_error('ftp_ssl_not_supported');
  114.                         }                      
  115.                 }
  116.                 else
  117.                 {
  118.                        
  119.                         $this->conn_id = @ftp_connect($this->hostname, $this->port);
  120.                 }
  121.                
  122.        
  123.                 if ($this->conn_id === FALSE)
  124.                 {
  125.                         if ($this->debug == TRUE)
  126.                         {
  127.                                 $this->_error('ftp_unable_to_connect');
  128.                         }              
  129.                         return FALSE;
  130.                 }
  131.  
  132.                 if ( ! $this->_login())
  133.                 {
  134.                         if ($this->debug == TRUE)
  135.                         {
  136.                                 $this->_error('ftp_unable_to_login');
  137.                         }              
  138.                         return FALSE;
  139.                 }
  140.                
  141.                 // Set passive mode if needed
  142.                 if ($this->passive == TRUE)
  143.                 {
  144.                         ftp_pasv($this->conn_id, TRUE);
  145.                 }
  146.  
  147.                 return TRUE;
  148.         }
  149.  
  150.  
  151.         function download($rempath, $locpath, $mode = 'auto')
  152.         {
  153.                 if ( ! $this->_is_conn())
  154.                 {
  155.                         return FALSE;
  156.                 }
  157.        
  158.                 // Set the mode if not specified
  159.                 if ($mode == 'auto')
  160.                 {
  161.                         // Get the file extension so we can set the upload type
  162.                         $ext = $this->_getext($rempath);
  163.                         $mode = $this->_settype($ext);
  164.                 }
  165.                
  166.                 $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  167.                
  168.                 $result = ftp_get($this->conn_id, $locpath, $rempath, $mode);
  169.  
  170.                 if ($result === FALSE)
  171.                 {
  172.                         if ($this->debug == TRUE)
  173.                         {
  174.                                 $this->_error('ftp_unable_to_download');
  175.                         }              
  176.                         return FALSE;          
  177.                 }
  178.                
  179.                 return TRUE;
  180.         }
  181.  
  182. }
  183. // END MY_FTP Class
  184. ?>