1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5
* An open source application development framework for PHP 5.1.6 or newer
8
* @author ExpressionEngine Dev Team
9
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10
* @license http://codeigniter.com/user_guide/license.html
11
* @link http://codeigniter.com
16
// ------------------------------------------------------------------------
21
* @package CodeIgniter
22
* @subpackage Libraries
24
* @author ExpressionEngine Dev Team
25
* @link http://codeigniter.com/user_guide/libraries/ftp.html
39
* Constructor - Sets Preferences
41
* The constructor can be passed an array of config values
43
public function __construct($config = array())
45
if (count($config) > 0)
47
$this->initialize($config);
50
log_message('debug', "FTP Class Initialized");
53
// --------------------------------------------------------------------
56
* Initialize preferences
62
function initialize($config = array())
64
foreach ($config as $key => $val)
66
if (isset($this->$key))
73
$this->hostname = preg_replace('|.+?://|', '', $this->hostname);
76
// --------------------------------------------------------------------
82
* @param array the connection values
85
function connect($config = array())
87
if (count($config) > 0)
89
$this->initialize($config);
92
if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port)))
94
if ($this->debug == TRUE)
96
$this->_error('ftp_unable_to_connect');
101
if ( ! $this->_login())
103
if ($this->debug == TRUE)
105
$this->_error('ftp_unable_to_login');
110
// Set passive mode if needed
111
if ($this->passive == TRUE)
113
ftp_pasv($this->conn_id, TRUE);
119
// --------------------------------------------------------------------
129
return @ftp_login($this->conn_id, $this->username, $this->password);
132
// --------------------------------------------------------------------
135
* Validates the connection ID
142
if ( ! is_resource($this->conn_id))
144
if ($this->debug == TRUE)
146
$this->_error('ftp_no_connection');
153
// --------------------------------------------------------------------
159
* The second parameter lets us momentarily turn off debugging so that
160
* this function can be used to test for the existence of a folder
161
* without throwing an error. There's no FTP equivalent to is_dir()
162
* so we do it by trying to change to a particular directory.
163
* Internally, this parameter is only used by the "mirror" function below.
170
function changedir($path = '', $supress_debug = FALSE)
172
if ($path == '' OR ! $this->_is_conn())
177
$result = @ftp_chdir($this->conn_id, $path);
179
if ($result === FALSE)
181
if ($this->debug == TRUE AND $supress_debug == FALSE)
183
$this->_error('ftp_unable_to_changedir');
191
// --------------------------------------------------------------------
200
function mkdir($path = '', $permissions = NULL)
202
if ($path == '' OR ! $this->_is_conn())
207
$result = @ftp_mkdir($this->conn_id, $path);
209
if ($result === FALSE)
211
if ($this->debug == TRUE)
213
$this->_error('ftp_unable_to_makdir');
218
// Set file permissions if needed
219
if ( ! is_null($permissions))
221
$this->chmod($path, (int)$permissions);
227
// --------------------------------------------------------------------
230
* Upload a file to the server
238
function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
240
if ( ! $this->_is_conn())
245
if ( ! file_exists($locpath))
247
$this->_error('ftp_no_source_file');
251
// Set the mode if not specified
254
// Get the file extension so we can set the upload type
255
$ext = $this->_getext($locpath);
256
$mode = $this->_settype($ext);
259
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
261
$result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);
263
if ($result === FALSE)
265
if ($this->debug == TRUE)
267
$this->_error('ftp_unable_to_upload');
272
// Set file permissions if needed
273
if ( ! is_null($permissions))
275
$this->chmod($rempath, (int)$permissions);
281
// --------------------------------------------------------------------
284
* Download a file from a remote server to the local server
292
function download($rempath, $locpath, $mode = 'auto')
294
if ( ! $this->_is_conn())
299
// Set the mode if not specified
302
// Get the file extension so we can set the upload type
303
$ext = $this->_getext($rempath);
304
$mode = $this->_settype($ext);
307
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
309
$result = @ftp_get($this->conn_id, $locpath, $rempath, $mode);
311
if ($result === FALSE)
313
if ($this->debug == TRUE)
315
$this->_error('ftp_unable_to_download');
323
// --------------------------------------------------------------------
326
* Rename (or move) a file
334
function rename($old_file, $new_file, $move = FALSE)
336
if ( ! $this->_is_conn())
341
$result = @ftp_rename($this->conn_id, $old_file, $new_file);
343
if ($result === FALSE)
345
if ($this->debug == TRUE)
347
$msg = ($move == FALSE) ? 'ftp_unable_to_rename' : 'ftp_unable_to_move';
357
// --------------------------------------------------------------------
367
function move($old_file, $new_file)
369
return $this->rename($old_file, $new_file, TRUE);
372
// --------------------------------------------------------------------
375
* Rename (or move) a file
381
function delete_file($filepath)
383
if ( ! $this->_is_conn())
388
$result = @ftp_delete($this->conn_id, $filepath);
390
if ($result === FALSE)
392
if ($this->debug == TRUE)
394
$this->_error('ftp_unable_to_delete');
402
// --------------------------------------------------------------------
405
* Delete a folder and recursively delete everything (including sub-folders)
406
* containted within it.
412
function delete_dir($filepath)
414
if ( ! $this->_is_conn())
419
// Add a trailing slash to the file path if needed
420
$filepath = preg_replace("/(.+?)\/*$/", "\\1/", $filepath);
422
$list = $this->list_files($filepath);
424
if ($list !== FALSE AND count($list) > 0)
426
foreach ($list as $item)
428
// If we can't delete the item it's probaly a folder so
429
// we'll recursively call delete_dir()
430
if ( ! @ftp_delete($this->conn_id, $item))
432
$this->delete_dir($item);
437
$result = @ftp_rmdir($this->conn_id, $filepath);
439
if ($result === FALSE)
441
if ($this->debug == TRUE)
443
$this->_error('ftp_unable_to_delete');
451
// --------------------------------------------------------------------
454
* Set file permissions
457
* @param string the file path
458
* @param string the permissions
461
function chmod($path, $perm)
463
if ( ! $this->_is_conn())
468
// Permissions can only be set when running PHP 5
469
if ( ! function_exists('ftp_chmod'))
471
if ($this->debug == TRUE)
473
$this->_error('ftp_unable_to_chmod');
478
$result = @ftp_chmod($this->conn_id, $perm, $path);
480
if ($result === FALSE)
482
if ($this->debug == TRUE)
484
$this->_error('ftp_unable_to_chmod');
492
// --------------------------------------------------------------------
495
* FTP List files in the specified directory
500
function list_files($path = '.')
502
if ( ! $this->_is_conn())
507
return ftp_nlist($this->conn_id, $path);
510
// ------------------------------------------------------------------------
513
* Read a directory and recreate it remotely
515
* This function recursively reads a folder and everything it contains (including
516
* sub-folders) and creates a mirror via FTP based on it. Whatever the directory structure
517
* of the original file path will be recreated on the server.
520
* @param string path to source with trailing slash
521
* @param string path to destination - include the base folder with trailing slash
524
function mirror($locpath, $rempath)
526
if ( ! $this->_is_conn())
531
// Open the local file path
532
if ($fp = @opendir($locpath))
534
// Attempt to open the remote file path.
535
if ( ! $this->changedir($rempath, TRUE))
537
// If it doesn't exist we'll attempt to create the direcotory
538
if ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath))
544
// Recursively read the local directory
545
while (FALSE !== ($file = readdir($fp)))
547
if (@is_dir($locpath.$file) && substr($file, 0, 1) != '.')
549
$this->mirror($locpath.$file."/", $rempath.$file."/");
551
elseif (substr($file, 0, 1) != ".")
553
// Get the file extension so we can se the upload type
554
$ext = $this->_getext($file);
555
$mode = $this->_settype($ext);
557
$this->upload($locpath.$file, $rempath.$file, $mode);
567
// --------------------------------------------------------------------
570
* Extract the file extension
576
function _getext($filename)
578
if (FALSE === strpos($filename, '.'))
583
$x = explode('.', $filename);
588
// --------------------------------------------------------------------
591
* Set the upload type
597
function _settype($ext)
616
return (in_array($ext, $text_types)) ? 'ascii' : 'binary';
619
// ------------------------------------------------------------------------
622
* Close the connection
625
* @param string path to source
626
* @param string path to destination
631
if ( ! $this->_is_conn())
636
@ftp_close($this->conn_id);
639
// ------------------------------------------------------------------------
642
* Display error message
648
function _error($line)
650
$CI =& get_instance();
651
$CI->lang->load('ftp');
652
show_error($CI->lang->line($line));
659
/* End of file Ftp.php */
660
/* Location: ./system/libraries/Ftp.php */
b'\\ No newline at end of file'