1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, 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 function CI_FTP($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 * Rename (or move) a file
292 function rename($old_file, $new_file, $move = FALSE)
294 if ( ! $this->_is_conn())
299 $result = @ftp_rename($this->conn_id, $old_file, $new_file);
301 if ($result === FALSE)
303 if ($this->debug == TRUE)
305 $msg = ($move == FALSE) ? 'ftp_unable_to_rename' : 'ftp_unable_to_move';
315 // --------------------------------------------------------------------
325 function move($old_file, $new_file)
327 return $this->rename($old_file, $new_file, TRUE);
330 // --------------------------------------------------------------------
333 * Rename (or move) a file
339 function delete_file($filepath)
341 if ( ! $this->_is_conn())
346 $result = @ftp_delete($this->conn_id, $filepath);
348 if ($result === FALSE)
350 if ($this->debug == TRUE)
352 $this->_error('ftp_unable_to_delete');
360 // --------------------------------------------------------------------
363 * Delete a folder and recursively delete everything (including sub-folders)
364 * containted within it.
370 function delete_dir($filepath)
372 if ( ! $this->_is_conn())
377 // Add a trailing slash to the file path if needed
378 $filepath = preg_replace("/(.+?)\/*$/", "\\1/", $filepath);
380 $list = $this->list_files($filepath);
382 if ($list !== FALSE AND count($list) > 0)
384 foreach ($list as $item)
386 // If we can't delete the item it's probaly a folder so
387 // we'll recursively call delete_dir()
388 if ( ! @ftp_delete($this->conn_id, $item))
390 $this->delete_dir($item);
395 $result = @ftp_rmdir($this->conn_id, $filepath);
397 if ($result === FALSE)
399 if ($this->debug == TRUE)
401 $this->_error('ftp_unable_to_delete');
409 // --------------------------------------------------------------------
412 * Set file permissions
415 * @param string the file path
416 * @param string the permissions
419 function chmod($path, $perm)
421 if ( ! $this->_is_conn())
426 // Permissions can only be set when running PHP 5
427 if ( ! function_exists('ftp_chmod'))
429 if ($this->debug == TRUE)
431 $this->_error('ftp_unable_to_chmod');
436 $result = @ftp_chmod($this->conn_id, $perm, $path);
438 if ($result === FALSE)
440 if ($this->debug == TRUE)
442 $this->_error('ftp_unable_to_chmod');
450 // --------------------------------------------------------------------
453 * FTP List files in the specified directory
458 function list_files($path = '.')
460 if ( ! $this->_is_conn())
465 return ftp_nlist($this->conn_id, $path);
468 // ------------------------------------------------------------------------
471 * Read a directory and recreate it remotely
473 * This function recursively reads a folder and everything it contains (including
474 * sub-folders) and creates a mirror via FTP based on it. Whatever the directory structure
475 * of the original file path will be recreated on the server.
478 * @param string path to source with trailing slash
479 * @param string path to destination - include the base folder with trailing slash
482 function mirror($locpath, $rempath)
484 if ( ! $this->_is_conn())
489 // Open the local file path
490 if ($fp = @opendir($locpath))
492 // Attempt to open the remote file path.
493 if ( ! $this->changedir($rempath, TRUE))
495 // If it doesn't exist we'll attempt to create the direcotory
496 if ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath))
502 // Recursively read the local directory
503 while (FALSE !== ($file = readdir($fp)))
505 if (@is_dir($locpath.$file) && substr($file, 0, 1) != '.')
507 $this->mirror($locpath.$file."/", $rempath.$file."/");
509 elseif (substr($file, 0, 1) != ".")
511 // Get the file extension so we can se the upload type
512 $ext = $this->_getext($file);
513 $mode = $this->_settype($ext);
515 $this->upload($locpath.$file, $rempath.$file, $mode);
525 // --------------------------------------------------------------------
528 * Extract the file extension
534 function _getext($filename)
536 if (FALSE === strpos($filename, '.'))
541 $x = explode('.', $filename);
546 // --------------------------------------------------------------------
549 * Set the upload type
555 function _settype($ext)
574 return (in_array($ext, $text_types)) ? 'ascii' : 'binary';
577 // ------------------------------------------------------------------------
580 * Close the connection
583 * @param string path to source
584 * @param string path to destination
589 if ( ! $this->_is_conn())
594 @ftp_close($this->conn_id);
597 // ------------------------------------------------------------------------
600 * Display error message
606 function _error($line)
608 $CI =& get_instance();
609 $CI->lang->load('ftp');
610 show_error($CI->lang->line($line));
617 /* End of file Ftp.php */
618 /* Location: ./system/libraries/Ftp.php */