Skip to content

Commit bf69595

Browse files
author
Phil Sturgeon
committed
Merge pull request bcit-ci#812 from narfbg/develop-ftp
Improve the FTP library
2 parents 402a527 + 0a75d6e commit bf69595

File tree

1 file changed

+42
-60
lines changed

1 file changed

+42
-60
lines changed

system/libraries/Ftp.php

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
22
/**
33
* CodeIgniter
44
*
55
* An open source application development framework for PHP 5.1.6 or newer
66
*
77
* NOTICE OF LICENSE
8-
*
8+
*
99
* Licensed under the Open Software License version 3.0
10-
*
10+
*
1111
* This source file is subject to the Open Software License (OSL 3.0) that is
1212
* bundled with this package in the files license.txt / license.rst. It is
1313
* also available through the world wide web at this URL:
@@ -38,13 +38,13 @@
3838
*/
3939
class CI_FTP {
4040

41-
var $hostname = '';
42-
var $username = '';
43-
var $password = '';
44-
var $port = 21;
45-
var $passive = TRUE;
46-
var $debug = FALSE;
47-
var $conn_id = FALSE;
41+
public $hostname = '';
42+
public $username = '';
43+
public $password = '';
44+
public $port = 21;
45+
public $passive = TRUE;
46+
public $debug = FALSE;
47+
public $conn_id = FALSE;
4848

4949

5050
/**
@@ -71,7 +71,7 @@ public function __construct($config = array())
7171
* @param array
7272
* @return void
7373
*/
74-
function initialize($config = array())
74+
public function initialize($config = array())
7575
{
7676
foreach ($config as $key => $val)
7777
{
@@ -94,7 +94,7 @@ function initialize($config = array())
9494
* @param array the connection values
9595
* @return bool
9696
*/
97-
function connect($config = array())
97+
public function connect($config = array())
9898
{
9999
if (count($config) > 0)
100100
{
@@ -136,7 +136,7 @@ function connect($config = array())
136136
* @access private
137137
* @return bool
138138
*/
139-
function _login()
139+
private function _login()
140140
{
141141
return @ftp_login($this->conn_id, $this->username, $this->password);
142142
}
@@ -149,7 +149,7 @@ function _login()
149149
* @access private
150150
* @return bool
151151
*/
152-
function _is_conn()
152+
private function _is_conn()
153153
{
154154
if ( ! is_resource($this->conn_id))
155155
{
@@ -179,7 +179,7 @@ function _is_conn()
179179
* @param bool
180180
* @return bool
181181
*/
182-
function changedir($path = '', $supress_debug = FALSE)
182+
public function changedir($path = '', $supress_debug = FALSE)
183183
{
184184
if ($path == '' OR ! $this->_is_conn())
185185
{
@@ -209,7 +209,7 @@ function changedir($path = '', $supress_debug = FALSE)
209209
* @param string
210210
* @return bool
211211
*/
212-
function mkdir($path = '', $permissions = NULL)
212+
public function mkdir($path = '', $permissions = NULL)
213213
{
214214
if ($path == '' OR ! $this->_is_conn())
215215
{
@@ -247,7 +247,7 @@ function mkdir($path = '', $permissions = NULL)
247247
* @param string
248248
* @return bool
249249
*/
250-
function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
250+
public function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
251251
{
252252
if ( ! $this->_is_conn())
253253
{
@@ -261,14 +261,14 @@ function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
261261
}
262262

263263
// Set the mode if not specified
264-
if ($mode == 'auto')
264+
if ($mode === 'auto')
265265
{
266266
// Get the file extension so we can set the upload type
267267
$ext = $this->_getext($locpath);
268268
$mode = $this->_settype($ext);
269269
}
270270

271-
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
271+
$mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;
272272

273273
$result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);
274274

@@ -301,22 +301,22 @@ function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
301301
* @param string
302302
* @return bool
303303
*/
304-
function download($rempath, $locpath, $mode = 'auto')
304+
public function download($rempath, $locpath, $mode = 'auto')
305305
{
306306
if ( ! $this->_is_conn())
307307
{
308308
return FALSE;
309309
}
310310

311311
// Set the mode if not specified
312-
if ($mode == 'auto')
312+
if ($mode === 'auto')
313313
{
314314
// Get the file extension so we can set the upload type
315315
$ext = $this->_getext($rempath);
316316
$mode = $this->_settype($ext);
317317
}
318318

319-
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
319+
$mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;
320320

321321
$result = @ftp_get($this->conn_id, $locpath, $rempath, $mode);
322322

@@ -343,7 +343,7 @@ function download($rempath, $locpath, $mode = 'auto')
343343
* @param bool
344344
* @return bool
345345
*/
346-
function rename($old_file, $new_file, $move = FALSE)
346+
public function rename($old_file, $new_file, $move = FALSE)
347347
{
348348
if ( ! $this->_is_conn())
349349
{
@@ -356,9 +356,7 @@ function rename($old_file, $new_file, $move = FALSE)
356356
{
357357
if ($this->debug == TRUE)
358358
{
359-
$msg = ($move == FALSE) ? 'ftp_unable_to_rename' : 'ftp_unable_to_move';
360-
361-
$this->_error($msg);
359+
$this->_error('ftp_unable_to_' . ($move == FALSE ? 'rename' : 'move'));
362360
}
363361
return FALSE;
364362
}
@@ -376,7 +374,7 @@ function rename($old_file, $new_file, $move = FALSE)
376374
* @param string
377375
* @return bool
378376
*/
379-
function move($old_file, $new_file)
377+
public function move($old_file, $new_file)
380378
{
381379
return $this->rename($old_file, $new_file, TRUE);
382380
}
@@ -390,7 +388,7 @@ function move($old_file, $new_file)
390388
* @param string
391389
* @return bool
392390
*/
393-
function delete_file($filepath)
391+
public function delete_file($filepath)
394392
{
395393
if ( ! $this->_is_conn())
396394
{
@@ -421,7 +419,7 @@ function delete_file($filepath)
421419
* @param string
422420
* @return bool
423421
*/
424-
function delete_dir($filepath)
422+
public function delete_dir($filepath)
425423
{
426424
if ( ! $this->_is_conn())
427425
{
@@ -470,23 +468,13 @@ function delete_dir($filepath)
470468
* @param string the permissions
471469
* @return bool
472470
*/
473-
function chmod($path, $perm)
471+
public function chmod($path, $perm)
474472
{
475473
if ( ! $this->_is_conn())
476474
{
477475
return FALSE;
478476
}
479477

480-
// Permissions can only be set when running PHP 5
481-
if ( ! function_exists('ftp_chmod'))
482-
{
483-
if ($this->debug == TRUE)
484-
{
485-
$this->_error('ftp_unable_to_chmod');
486-
}
487-
return FALSE;
488-
}
489-
490478
$result = @ftp_chmod($this->conn_id, $perm, $path);
491479

492480
if ($result === FALSE)
@@ -509,7 +497,7 @@ function chmod($path, $perm)
509497
* @access public
510498
* @return array
511499
*/
512-
function list_files($path = '.')
500+
public function list_files($path = '.')
513501
{
514502
if ( ! $this->_is_conn())
515503
{
@@ -533,7 +521,7 @@ function list_files($path = '.')
533521
* @param string path to destination - include the base folder with trailing slash
534522
* @return bool
535523
*/
536-
function mirror($locpath, $rempath)
524+
public function mirror($locpath, $rempath)
537525
{
538526
if ( ! $this->_is_conn())
539527
{
@@ -543,24 +531,20 @@ function mirror($locpath, $rempath)
543531
// Open the local file path
544532
if ($fp = @opendir($locpath))
545533
{
546-
// Attempt to open the remote file path.
547-
if ( ! $this->changedir($rempath, TRUE))
534+
// Attempt to open the remote file path and try to create it, if it doesn't exist
535+
if ( ! $this->changedir($rempath, TRUE) AND ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath)))
548536
{
549-
// If it doesn't exist we'll attempt to create the direcotory
550-
if ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath))
551-
{
552-
return FALSE;
553-
}
537+
return FALSE;
554538
}
555539

556540
// Recursively read the local directory
557541
while (FALSE !== ($file = readdir($fp)))
558542
{
559-
if (@is_dir($locpath.$file) && substr($file, 0, 1) != '.')
543+
if (@is_dir($locpath.$file) && $file[0] !== '.')
560544
{
561545
$this->mirror($locpath.$file."/", $rempath.$file."/");
562546
}
563-
elseif (substr($file, 0, 1) != ".")
547+
elseif ($file[0] !== ".")
564548
{
565549
// Get the file extension so we can se the upload type
566550
$ext = $this->_getext($file);
@@ -585,7 +569,7 @@ function mirror($locpath, $rempath)
585569
* @param string
586570
* @return string
587571
*/
588-
function _getext($filename)
572+
private function _getext($filename)
589573
{
590574
if (FALSE === strpos($filename, '.'))
591575
{
@@ -606,7 +590,7 @@ function _getext($filename)
606590
* @param string
607591
* @return string
608592
*/
609-
function _settype($ext)
593+
private function _settype($ext)
610594
{
611595
$text_types = array(
612596
'txt',
@@ -634,18 +618,16 @@ function _settype($ext)
634618
* Close the connection
635619
*
636620
* @access public
637-
* @param string path to source
638-
* @param string path to destination
639621
* @return bool
640622
*/
641-
function close()
623+
public function close()
642624
{
643625
if ( ! $this->_is_conn())
644626
{
645627
return FALSE;
646628
}
647629

648-
@ftp_close($this->conn_id);
630+
return @ftp_close($this->conn_id);
649631
}
650632

651633
// ------------------------------------------------------------------------
@@ -655,9 +637,9 @@ function close()
655637
*
656638
* @access private
657639
* @param string
658-
* @return bool
640+
* @return void
659641
*/
660-
function _error($line)
642+
private function _error($line)
661643
{
662644
$CI =& get_instance();
663645
$CI->lang->load('ftp');
@@ -669,4 +651,4 @@ function _error($line)
669651
// END FTP Class
670652

671653
/* End of file Ftp.php */
672-
/* Location: ./system/libraries/Ftp.php */
654+
/* Location: ./system/libraries/Ftp.php */

0 commit comments

Comments
 (0)