<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ftp &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/ftp/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Mon, 01 Oct 2012 08:30:25 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>
	<item>
		<title>ftp上传类-php</title>
		<link>https://www.mudbest.com/ftp%e4%b8%8a%e4%bc%a0%e7%b1%bb-php/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Mon, 05 Mar 2012 07:06:18 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ftp]]></category>
		<category><![CDATA[上传类]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=882</guid>

					<description><![CDATA[FTP基本操作： 1 ) 登陆 connect 2 ) 当前目录文件列表 filelist 3 ) 目录改变 ...]]></description>
										<content:encoded><![CDATA[<p>FTP基本操作：<br />
 1 ) 登陆                     connect<br />
 2 ) 当前目录文件列表    filelist<br />
 3 ) 目录改变               chgdir<br />
 4 ) 重命名/移动           rename<br />
 5 ) 创建文件夹            mkdir<br />
 6 ) 删除                     delete_dir/delete_file<br />
 7 ) 上传                     upload<br />
 8 ) 下载                     download<br />
<span id="more-882"></span><br />
ftp.php</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/**
 * FTP基本操作：
 * 1) 登陆; 			connect
 * 2) 当前目录文件列表;  filelist
 * 3) 目录改变;			chgdir
 * 4) 重命名/移动;		rename
 * 5) 创建文件夹;		mkdir
 * 6) 删除;				delete_dir/delete_file
 * 7) 上传;				upload
 * 8) 下载				download
 *
 */
class Ftp {

	private $hostname	= '';
	private $username	= '';
	private $password	= '';
	private $port 		= 21;
	private $passive 	= TRUE;
	private $debug		= TRUE;
	private $conn_id 	= FALSE;
	
	/**
	 * 构造函数
	 *
	 * @param	array	配置数组 : $config = array('hostname'=&gt;'','username'=&gt;'','password'=&gt;'','port'=&gt;''...);
	 */
	public function __construct($config = array()) {
		if(count($config) &gt; 0) {
			$this-&gt;_init($config);
		}
	}
	
	/**
	 * FTP连接
	 *
	 * @access 	public
	 * @param 	array 	配置数组
	 * @return	boolean
	 */
	public function connect($config = array()) {
		if(count($config) &gt; 0) {
			$this-&gt;_init($config);
		}
		
		if(FALSE === ($this-&gt;conn_id = @ftp_connect($this-&gt;hostname,$this-&gt;port))) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_unable_to_connect&quot;);
			}
			return FALSE;
		}
		
		if( ! $this-&gt;_login()) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_unable_to_login&quot;);
			}
			return FALSE;
		}
		
		if($this-&gt;passive === TRUE) {
			ftp_pasv($this-&gt;conn_id, TRUE);
		}
		
		return TRUE;
	}

	
	/**
	 * 目录改变
	 *
	 * @access 	public
	 * @param 	string 	目录标识(ftp)
	 * @param	boolean	
	 * @return	boolean
	 */
	public function chgdir($path = '', $supress_debug = FALSE) {
		if($path == '' OR ! $this-&gt;_isconn()) {
			return FALSE;
		}
		
		$result = @ftp_chdir($this-&gt;conn_id, $path);
		
		if($result === FALSE) {
			if($this-&gt;debug === TRUE AND $supress_debug == FALSE) {
				$this-&gt;_error(&quot;ftp_unable_to_chgdir:dir&#x5B;&quot;.$path.&quot;]&quot;);
			}
			return FALSE;
		}
		
		return TRUE;
	}
	
	/**
	 * 目录生成
	 *
	 * @access 	public
	 * @param 	string 	目录标识(ftp)
	 * @param	int  	文件权限列表	
	 * @return	boolean
	 */
	public function mkdir($path = '', $permissions = NULL) {
		if($path == '' OR ! $this-&gt;_isconn()) {
			return FALSE;
		}
		
		$result = @ftp_mkdir($this-&gt;conn_id, $path);
		
		if($result === FALSE) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_unable_to_mkdir:dir&#x5B;&quot;.$path.&quot;]&quot;);
			}
			return FALSE;
		}
		
		if( ! is_null($permissions)) {
			$this-&gt;chmod($path,(int)$permissions);
		}
		
		return TRUE;
	}
	
	/**
	 * 上传
	 *
	 * @access 	public
	 * @param 	string 	本地目录标识
	 * @param	string	远程目录标识(ftp)
	 * @param	string	上传模式 auto || ascii
	 * @param	int		上传后的文件权限列表	
	 * @return	boolean
	 */
	public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
		if( ! $this-&gt;_isconn()) {
			return FALSE;
		}
		
		if( ! file_exists($localpath)) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_no_source_file:&quot;.$localpath);
			}
			return FALSE;
		}
		
		if($mode == 'auto') {
			$ext = $this-&gt;_getext($localpath);
			$mode = $this-&gt;_settype($ext);
		}
		
		$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
		
		$result = @ftp_put($this-&gt;conn_id, $remotepath, $localpath, $mode);
		
		if($result === FALSE) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_unable_to_upload:localpath&#x5B;&quot;.$localpath.&quot;]/remotepath&#x5B;&quot;.$remotepath.&quot;]&quot;);
			}
			return FALSE;
		}
		
		if( ! is_null($permissions)) {
			$this-&gt;chmod($remotepath,(int)$permissions);
		}
		
		return TRUE;
	}
	
	/**
	 * 下载
	 *
	 * @access 	public
	 * @param 	string 	远程目录标识(ftp)
	 * @param	string	本地目录标识
	 * @param	string	下载模式 auto || ascii	
	 * @return	boolean
	 */
	public function download($remotepath, $localpath, $mode = 'auto') {
		if( ! $this-&gt;_isconn()) {
			return FALSE;
		}
		
		if($mode == 'auto') {
			$ext = $this-&gt;_getext($remotepath);
			$mode = $this-&gt;_settype($ext);
		}
		
		$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
		
		$result = @ftp_get($this-&gt;conn_id, $localpath, $remotepath, $mode);
		
		if($result === FALSE) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_unable_to_download:localpath&#x5B;&quot;.$localpath.&quot;]-remotepath&#x5B;&quot;.$remotepath.&quot;]&quot;);
			}
			return FALSE;
		}
		
		return TRUE;
	}
	
	/**
	 * 重命名/移动
	 *
	 * @access 	public
	 * @param 	string 	远程目录标识(ftp)
	 * @param	string	新目录标识
	 * @param	boolean	判断是重命名(FALSE)还是移动(TRUE)	
	 * @return	boolean
	 */
	public function rename($oldname, $newname, $move = FALSE) {
		if( ! $this-&gt;_isconn()) {
			return FALSE;
		}
		
		$result = @ftp_rename($this-&gt;conn_id, $oldname, $newname);
		
		if($result === FALSE) {
			if($this-&gt;debug === TRUE) {
				$msg = ($move == FALSE) ? &quot;ftp_unable_to_rename&quot; : &quot;ftp_unable_to_move&quot;;
				$this-&gt;_error($msg);
			}
			return FALSE;
		}
		
		return TRUE;
	}
	
	/**
	 * 删除文件
	 *
	 * @access 	public
	 * @param 	string 	文件标识(ftp)
	 * @return	boolean
	 */
	public function delete_file($file) {
		if( ! $this-&gt;_isconn()) {
			return FALSE;
		}
		
		$result = @ftp_delete($this-&gt;conn_id, $file);
		
		if($result === FALSE) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_unable_to_delete_file:file&#x5B;&quot;.$file.&quot;]&quot;);
			}
			return FALSE;
		}
		
		return TRUE;
	}
	
	/**
	 * 删除文件夹
	 *
	 * @access 	public
	 * @param 	string 	目录标识(ftp)
	 * @return	boolean
	 */
	public function delete_dir($path) {
		if( ! $this-&gt;_isconn()) {
			return FALSE;
		}
		
		//对目录宏的'/'字符添加反斜杠'\'
		$path = preg_replace(&quot;/(.+?)\/*$/&quot;, &quot;\\1/&quot;, $path);
	
		//获取目录文件列表
		$filelist = $this-&gt;filelist($path);
		
		if($filelist !== FALSE AND count($filelist) &gt; 0) {
			foreach($filelist as $item) {
				//如果我们无法删除,那么就可能是一个文件夹
				//所以我们递归调用delete_dir()
				if( ! @delete_file($item)) {
					$this-&gt;delete_dir($item);
				}
			}
		}
		
		//删除文件夹(空文件夹)
		$result = @ftp_rmdir($this-&gt;conn_id, $path);
		
		if($result === FALSE) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_unable_to_delete_dir:dir&#x5B;&quot;.$path.&quot;]&quot;);
			}
			return FALSE;
		}
		
		return TRUE;
	}
	
	/**
	 * 修改文件权限
	 *
	 * @access 	public
	 * @param 	string 	目录标识(ftp)
	 * @return	boolean
	 */
	public function chmod($path, $perm) {
		if( ! $this-&gt;_isconn()) {
			return FALSE;
		}
		
		//只有在PHP5中才定义了修改权限的函数(ftp)
		if( ! function_exists('ftp_chmod')) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_unable_to_chmod(function)&quot;);
			}
			return FALSE;
		}
		
		$result = @ftp_chmod($this-&gt;conn_id, $perm, $path);
		
		if($result === FALSE) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_unable_to_chmod:path&#x5B;&quot;.$path.&quot;]-chmod&#x5B;&quot;.$perm.&quot;]&quot;);
			}
			return FALSE;
		}
		return TRUE;
	}
	
	/**
	 * 获取目录文件列表
	 *
	 * @access 	public
	 * @param 	string 	目录标识(ftp)
	 * @return	array
	 */
	public function filelist($path = '.') {
		if( ! $this-&gt;_isconn()) {
			return FALSE;
		}
		
		return ftp_nlist($this-&gt;conn_id, $path);
	}
	
	/**
	 * 关闭FTP
	 *
	 * @access 	public
	 * @return	boolean
	 */
	public function close() {
		if( ! $this-&gt;_isconn()) {
			return FALSE;
		}
		
		return @ftp_close($this-&gt;conn_id);
	}
	
	/**
	 * FTP成员变量初始化
	 *
	 * @access	private
	 * @param	array	配置数组	 
	 * @return	void
	 */
	private function _init($config = array()) {
		foreach($config as $key =&gt; $val) {
			if(isset($this-&gt;$key)) {
				$this-&gt;$key = $val;
			}
		}

		//特殊字符过滤
		$this-&gt;hostname = preg_replace('|.+?://|','',$this-&gt;hostname);
	}
	
	/**
	 * FTP登陆
	 *
	 * @access 	private
	 * @return	boolean
	 */
	private function _login() {
		return @ftp_login($this-&gt;conn_id, $this-&gt;username, $this-&gt;password);
	}
	
	/**
	 * 判断con_id
	 *
	 * @access 	private
	 * @return	boolean
	 */
	private function _isconn() {
		if( ! is_resource($this-&gt;conn_id)) {
			if($this-&gt;debug === TRUE) {
				$this-&gt;_error(&quot;ftp_no_connection&quot;);
			}
			return FALSE;
		}
		return TRUE;
	}
	
	/**
	 * 从文件名中获取后缀扩展
	 *
	 * @access 	private
	 * @param 	string 	目录标识
	 * @return	string
	 */
	private function _getext($filename) {
		if(FALSE === strpos($filename, '.')) {
			return 'txt';
		}
		
		$extarr = explode('.', $filename);
		return end($extarr);
	}
	
	/**
	 * 从后缀扩展定义FTP传输模式  ascii 或 binary
	 *
	 * @access 	private
	 * @param 	string 	后缀扩展
	 * @return	string
	 */
	private function _settype($ext) {
		$text_type = array (
							'txt',
							'text',
							'php',
							'phps',
							'php4',
							'js',
							'css',
							'htm',
							'html',
							'phtml',
							'shtml',
							'log',
							'xml'
							);
		
		return (in_array($ext, $text_type)) ? 'ascii' : 'binary';
	}
	
	/**
	 * 错误日志记录
	 *
	 * @access 	prvate
	 * @return	boolean
	 */
	private function _error($msg) {
		return @file_put_contents('ftp_err.log', &quot;date&#x5B;&quot;.date(&quot;Y-m-d H:i:s&quot;).&quot;]-hostname&#x5B;&quot;.$this-&gt;hostname.&quot;]-username&#x5B;&quot;.$this-&gt;username.&quot;]-password&#x5B;&quot;.$this-&gt;password.&quot;]-msg&#x5B;&quot;.$msg.&quot;]\n&quot;, FILE_APPEND);
	}
}
?&gt;
</pre>
<p>实例部分：<br />
index.php</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
require_once('ftp.php');

$config = array(
            'hostname' =&gt; '192.168.1.254',
            'username' =&gt; '51somaccdata',
            'password' =&gt; 'Soma123456',
            'port' =&gt; 21
                );

$ftp = new Ftp();

$ftp-&gt;connect($config);
$ftp-&gt;upload('ftp_err.log','ftp_upload.log');  //上传
$ftp-&gt;download('ftp_upload.log','ftp_download.log');  //下载
?&gt;
</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
