1.需求
了解php的ftp使用
2.例子
使用CI封装好的ftp类库
上传 $this->load->library('ftp'); $config['hostname'] = 'ftp.example.com'; $config['username'] = 'your-username'; $config['password'] = 'your-password'; $config['debug'] = TRUE; $this->ftp->connect($config); //路径要绝对路径 $this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775); $this->ftp->close();
下载文件列表 $this->load->library('ftp'); $config['hostname'] = 'ftp.example.com'; $config['username'] = 'your-username'; $config['password'] = 'your-password'; $config['debug'] = TRUE; $this->ftp->connect($config); $list = $this->ftp->list_files('/public_html/'); print_r($list); $this->ftp->close();
3.自己写的class
<?php class ftp{ public $hostname = ''; public $username = ''; public $password = ''; public $port = 21; public $passive = TRUE; public $debug = FALSE; protected $conn_id; public function __construct($config =array()) { if(count($config)>0) { $this->initialize($config); } } public function initialize($config) { foreach ($config as $key => $val) { if (isset($this->$key)) { $this->$key = $val; } } $this->hostname = preg_replace('|.+?://|', '', $this->hostname); } public function connect($config = array()) { if (count($config) > 0) { $this->initialize($config); } if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port))) { if ($this->debug === TRUE) { echo "ftp_unable_to_connect"; exit(); } return FALSE; } if ( ! $this->_login()) { if ($this->debug === TRUE) { echo "ftp_unable_to_login"; exit(); } return FALSE; } if ($this->passive === TRUE) { ftp_pasv($this->conn_id, TRUE); } return TRUE; } protected function _login() { return @ftp_login($this->conn_id, $this->username, $this->password); } protected function _is_conn() { if ( ! is_resource($this->conn_id)) { if ($this->debug === TRUE) { echo 'ftp_no_connection'; exit(); } return FALSE; } return TRUE; } public function changedir($path, $suppress_debug = FALSE) { if ( ! $this->_is_conn()) { return FALSE; } $result = @ftp_chdir($this->conn_id, $path); if ($result === FALSE) { if ($this->debug === TRUE && $suppress_debug === FALSE) { echo 'ftp_unable_to_changedir'; exit(); } return FALSE; } return TRUE; } public function mkdir($path, $permissions = NULL) { if ($path === '' OR ! $this->_is_conn()) { return FALSE; } $result = @ftp_mkdir($this->conn_id, $path); if ($result === FALSE) { if ($this->debug === TRUE) { echo 'ftp_unable_to_mkdir'; exit(); } return FALSE; } if ($permissions !== NULL) { $this->chmod($path, (int) $permissions); } return TRUE; } public function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL) { if ( ! $this->_is_conn()) { return FALSE; } if ( ! file_exists($locpath)) { echo 'ftp_no_source_file'; return FALSE; } if ($mode === 'auto') { // Get the file extension so we can set the upload type $ext = $this->_getext($locpath); $mode = $this->_settype($ext); } $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY; $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode); if ($result === FALSE) { if ($this->debug === TRUE) { echo 'ftp_unable_to_upload'; exit(); } return FALSE; } if ($permissions !== NULL) { $this->chmod($rempath, (int) $permissions); } return TRUE; } public function download($rempath, $locpath, $mode = 'auto') { if ( ! $this->_is_conn()) { return FALSE; } if ($mode === 'auto') { // Get the file extension so we can set the upload type $ext = $this->_getext($rempath); $mode = $this->_settype($ext); } $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY; $result = @ftp_get($this->conn_id, $locpath, $rempath, $mode); if ($result === FALSE) { if ($this->debug === TRUE) { echo 'ftp_unable_to_download'; exit(); } return FALSE; } return TRUE; } public function delete_file($filepath) { if ( ! $this->_is_conn()) { return FALSE; } $result = @ftp_delete($this->conn_id, $filepath); if ($result === FALSE) { if ($this->debug === TRUE) { echo 'ftp_unable_to_delete'; exit(); } return FALSE; } return TRUE; } public function chmod($path, $perm) { if ( ! $this->_is_conn()) { return FALSE; } if (@ftp_chmod($this->conn_id, $perm, $path) === FALSE) { if ($this->debug === TRUE) { echo 'ftp_unable_to_chmod'; exit(); } return FALSE; } return TRUE; } protected function _getext($filename) { return (($dot = strrpos($filename, '.')) === FALSE) ? 'txt' : substr($filename, $dot + 1); } protected function _settype($ext) { return in_array($ext, array('txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'), TRUE) ? 'ascii' : 'binary'; } public function close() { return $this->_is_conn() ? @ftp_close($this->conn_id) : FALSE; } } $obj = new ftp(); $config['hostname'] = 'http://xia.com'; $config['username'] = 'web'; $config['password'] = 'web'; $config['debug'] = TRUE; $obj->connect($config); //路径要绝对路径 $obj->upload("C:\Users\Administrator\Desktop\e.txt", '/www/3.txt', 'ascii', 0775); $obj->close();
参考资料:
http://codeigniter.org.cn/user_guide/libraries/ftp.html
http://php.net/manual/zh/book.ftp.php