php FTP类详解

  • 2018-03-12 10:58:58
  • 开发
  • 22
  • shevechco

FTP是一种文件传输协议,它支持两种模式,一种方式叫做Standard (也就是Active,主动方式),一种是 Passive (也就是PASV,被动方式)。 Standard模式 FTP 的客户端发送 PORT 命令到FTP server。Passive模式FTP的客户端发送 PASV命令到 FTP Server。
下面介绍一个这两种方式的工作原理:

Standard模式
FTP 客户端首先和FTP Server的TCP 21端口建立连接,通过这个通道 发送命令,客户端需要接收数据的时候在这个通道上发送PORT命令。 PORT命令包含 了客户端用什么端口接收数据。在传送数据的时候,服务器端通过自己的TCP 20端口发送数据。 FTP server必须和客户端建立一个新的连接用来传送数据。

Passive模式
在建立控制通道的时候和Standard模式类似,当客户端通过这个通道发送PASV 命令的时候,FTP server打开一个位于1024和5000之间的随机端口并且通知 客户端在这个端口上传送数据的请求,然后FTP server 将通过这个端口进行数据的传送,这个时候FTP server不再需要建立一个新的和客户端之间的连接。
使用PHP操作FTP-用法

01.
<?  
02.
// 联接FTP服务器  
03.
$conn = ftp_connect(ftp.server.com);  
04.
// 使用username和password登录  
05.
ftp_login($conn, “john”, “doe”);  
06.
// 获取远端系统类型  
07.
ftp_systype($conn);  
08.
// 列示文件  
09.
$filelist = ftp_nlist($conn, “.”);  
10.
// 下载文件  
11.
ftp_get($conn, “data.zip”, “data.zip”, FTP_BINARY);  
12.
// 关闭联接  
13.
ftp_quit($conn);  
14.
//初结化一个FTP联接,PHP提供了ftp_connect()这个函数,它使用主机名称和端口作为参数。在上面的例子里,主机名字为 “ftp.server.com”;如果端口没指定,PHP将会使用“21”作为缺省端口来建立联接。  
15.
//联接成功后ftp_connect()传回一个handle句柄;这个handle将被以后使用的FTP函数使用。  
16.
$conn = ftp_connect(ftp.server.com);  
17.
//一旦建立联接,使用ftp_login()发送一个用户名称和用户密码。你可以看到,这个函数ftp_login()使用了 ftp_connect()函数传来的handle,以确定用户名和密码能被提交到正确的服务器。  
18.
ftp_login($conn, “john”, “doe”);  
19.
// close connection  
20.
ftp_quit($conn);  
21.
//登录了FTP服务器,PHP提供了一些函数,它们能获取一些关于系统和文件以及目录的信息。  
22.
ftp_pwd()  
23.
//获取当前所在的目录  
24.
$here = ftp_pwd($conn);  
25.
//获取服务器端系统信息ftp_systype()  
26.
$server_os = ftp_systype($conn);  
27.
//被动模式(PASV)的开关,打开或关闭PASV(1表示开)  
28.
ftp_pasv($conn, 1);  
29.
//进入目录中用ftp_chdir()函数,它接受一个目录名作为参数。  
30.
ftp_chdir($conn, “public_html”);  
31.
//回到所在的目录父目录用ftp_cdup()实现  
32.
ftp_cdup($conn);  
33.
//建立或移动一个目录,这要使用ftp_mkdir()和ftp_rmdir()函数;注意:ftp_mkdir()建立成功的话,就会返回新建立的目录名。  
34.
ftp_mkdir($conn, “test”);  
35.
ftp_rmdir($conn, “test”);  
36.
//上传文件,ftp_put()函数能很好的胜任,它需要你指定一个本地文件名,上传后的文件名以及传输的类型。比方说:如果你想上传 “abc.txt”这个文件,上传后命名为“xyz.txt”,命令应该是这样:  
37.
ftp_put($conn, “xyz.txt”, “abc.txt”, FTP_ASCII);  
38.
//下载文件:PHP所提供的函数是ftp_get(),它也需要一个服务器上文件名,下载后的文件名,以及传输类型作为参数,例如:服务器端文件为his.zip,你想下载至本地机,并命名为hers.zip,命令如下:  
39.
ftp_get($conn, “hers.zip”, “his.zip”, FTP_BINARY);  
40.
//PHP提供两种方法:一种是简单列示文件名和目录,另一种就是详细的列示文件的大小,权限,创立时间等信息。  
41.
//第一种使用ftp_nlist()函数,第二种用ftp_rawlist().两种函数都需要一个目录名做为参数,都返回目录列做为一个数组,数组的每一个元素相当于列表的一行。  
42.
$filelist = ftp_nlist($conn, “.”);  
43.
//函数ftp_size(),它返回你所指定的文件的大小,使用BITES作为单位。要指出的是,如果它返回的是 “-1”的话,意味着这是一个目录  
44.
$filelist = ftp_size($conn, “data.zip”);  
45.
?>
  

FTP类 

01.
<?php
02.
/**
03.
 * 仿写CodeIgniter的FTP类
04.
 * FTP基本操作:
05.
 * 1) 登陆;    connect
06.
 * 2) 当前目录文件列表;  filelist
07.
 * 3) 目录改变;   chgdir
08.
 * 4) 重命名/移动;  rename
09.
 * 5) 创建文件夹;  mkdir
10.
 * 6) 删除;    delete_dir/delete_file
11.
 * 7) 上传;    upload
12.
 * 8) 下载    download
13.
 *
14.
 * @author quanshuidingdang
15.
 */
16.
class Ftp {
17.
 private $hostname = '';
18.
 private $username = '';
19.
 private $password = '';
20.
 private $port   = 21;
21.
 private $passive  = TRUE;
22.
 private $debug  = TRUE;
23.
 private $conn_id  = FALSE;
24.
 /**
25.
  * 构造函数
26.
  *
27.
  * @param array 配置数组 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...);
28.
  */
29.
 public function __construct($config = array()) {
30.
  if(count($config) > 0) {
31.
   $this->_init($config);
32.
  }
33.
 }
34.
 /**
35.
  * FTP连接
36.
  *
37.
  * @access  public
38.
  * @param  array  配置数组
39.
  * @return boolean
40.
  */
41.
 public function connect($config = array()) {
42.
  if(count($config) > 0) {
43.
   $this->_init($config);
44.
  }
45.
  if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {
46.
   if($this->debug === TRUE) {
47.
    $this->_error("ftp_unable_to_connect");
48.
   }
49.
   return FALSE;
50.
  }
51.
  if( ! $this->_login()) {
52.
   if($this->debug === TRUE) {
53.
    $this->_error("ftp_unable_to_login");
54.
   }
55.
   return FALSE;
56.
  }
57.
  if($this->passive === TRUE) {
58.
   ftp_pasv($this->conn_id, TRUE);
59.
  }
60.
  return TRUE;
61.
 }
62.
 /**
63.
  * 目录改变
64.
  *
65.
  * @access  public
66.
  * @param  string  目录标识(ftp)
67.
  * @param boolean 
68.
  * @return boolean
69.
  */
70.
 public function chgdir($path = '', $supress_debug = FALSE) {
71.
  if($path == '' OR ! $this->_isconn()) {
72.
   return FALSE;
73.
  }
74.
  $result = @ftp_chdir($this->conn_id, $path);
75.
  if($result === FALSE) {
76.
   if($this->debug === TRUE AND $supress_debug == FALSE) {
77.
    $this->_error("ftp_unable_to_chgdir:dir[".$path."]");
78.
   }
79.
   return FALSE;
80.
  }
81.
  return TRUE;
82.
 }
83.
 /**
84.
  * 目录生成
85.
  *
86.
  * @access  public
87.
  * @param  string  目录标识(ftp)
88.
  * @param int   文件权限列表 
89.
  * @return boolean
90.
  */
91.
 public function mkdir($path = '', $permissions = NULL) {
92.
  if($path == '' OR ! $this->_isconn()) {
93.
   return FALSE;
94.
  }
95.
  $result = @ftp_mkdir($this->conn_id, $path);
96.
  if($result === FALSE) {
97.
   if($this->debug === TRUE) {
98.
    $this->_error("ftp_unable_to_mkdir:dir[".$path."]");
99.
   }
100.
   return FALSE;
101.
  }
102.
  if( ! is_null($permissions)) {
103.
   $this->chmod($path,(int)$permissions);
104.
  }
105.
  return TRUE;
106.
 }
107.
 /**
108.
  * 上传
109.
  *
110.
  * @access  public
111.
  * @param  string  本地目录标识
112.
  * @param string 远程目录标识(ftp)
113.
  * @param string 上传模式 auto || ascii
114.
  * @param int  上传后的文件权限列表 
115.
  * @return boolean
116.
  */
117.
 public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
118.
  if( ! $this->_isconn()) {
119.
   return FALSE;
120.
  }
121.
  if( ! file_exists($localpath)) {
122.
   if($this->debug === TRUE) {
123.
    $this->_error("ftp_no_source_file:".$localpath);
124.
   }
125.
   return FALSE;
126.
  }
127.
  if($mode == 'auto') {
128.
   $ext = $this->_getext($localpath);
129.
   $mode = $this->_settype($ext);
130.
  }
131.
  $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
132.
  $result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode);
133.
  if($result === FALSE) {
134.
   if($this->debug === TRUE) {
135.
    $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");
136.
   }
137.
   return FALSE;
138.
  }
139.
  if( ! is_null($permissions)) {
140.
   $this->chmod($remotepath,(int)$permissions);
141.
  }
142.
  return TRUE;
143.
 }
144.
 /**
145.
  * 下载
146.
  *
147.
  * @access  public
148.
  * @param  string  远程目录标识(ftp)
149.
  * @param string 本地目录标识
150.
  * @param string 下载模式 auto || ascii 
151.
  * @return boolean
152.
  */
153.
 public function download($remotepath, $localpath, $mode = 'auto') {
154.
  if( ! $this->_isconn()) {
155.
   return FALSE;
156.
  }
157.
  if($mode == 'auto') {
158.
   $ext = $this->_getext($remotepath);
159.
   $mode = $this->_settype($ext);
160.
  }
161.
  $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
162.
  $result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);
163.
  if($result === FALSE) {
164.
   if($this->debug === TRUE) {
165.
    $this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");
166.
   }
167.
   return FALSE;
168.
  }
169.
  return TRUE;
170.
 }
171.
 /**
172.
  * 重命名/移动
173.
  *
174.
  * @access  public
175.
  * @param  string  远程目录标识(ftp)
176.
  * @param string 新目录标识
177.
  * @param boolean 判断是重命名(FALSE)还是移动(TRUE) 
178.
  * @return boolean
179.
  */
180.
 public function rename($oldname, $newname, $move = FALSE) {
181.
  if( ! $this->_isconn()) {
182.
   return FALSE;
183.
  }
184.
  $result = @ftp_rename($this->conn_id, $oldname, $newname);
185.
  if($result === FALSE) {
186.
   if($this->debug === TRUE) {
187.
    $msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move";
188.
    $this->_error($msg);
189.
   }
190.
   return FALSE;
191.
  }
192.
  return TRUE;
193.
 }
194.
 /**
195.
  * 删除文件
196.
  *
197.
  * @access  public
198.
  * @param  string  文件标识(ftp)
199.
  * @return boolean
200.
  */
201.
 public function delete_file($file) {
202.
  if( ! $this->_isconn()) {
203.
   return FALSE;
204.
  }
205.
  $result = @ftp_delete($this->conn_id, $file);
206.
  if($result === FALSE) {
207.
   if($this->debug === TRUE) {
208.
    $this->_error("ftp_unable_to_delete_file:file[".$file."]");
209.
   }
210.
   return FALSE;
211.
  }
212.
  return TRUE;
213.
 }
214.
 /**
215.
  * 删除文件夹
216.
  *
217.
  * @access  public
218.
  * @param  string  目录标识(ftp)
219.
  * @return boolean
220.
  */
221.
 public function delete_dir($path) {
222.
  if( ! $this->_isconn()) {
223.
   return FALSE;
224.
  }
225.
  //对目录宏的'/'字符添加反斜杠'\'
226.
  $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
227.
  //获取目录文件列表
228.
  $filelist = $this->filelist($path);
229.
  if($filelist !== FALSE AND count($filelist) > 0) {
230.
   foreach($filelist as $item) {
231.
    //如果我们无法删除,那么就可能是一个文件夹
232.
    //所以我们递归调用delete_dir()
233.
    if( ! @delete_file($item)) {
234.
     $this->delete_dir($item);
235.
    }
236.
   }
237.
  }
238.
  //删除文件夹(空文件夹)
239.
  $result = @ftp_rmdir($this->conn_id, $path);
240.
  if($result === FALSE) {
241.
   if($this->debug === TRUE) {
242.
    $this->_error("ftp_unable_to_delete_dir:dir[".$path."]");
243.
   }
244.
   return FALSE;
245.
  }
246.
  return TRUE;
247.
 }
248.
 /**
249.
  * 修改文件权限
250.
  *
251.
  * @access  public
252.
  * @param  string  目录标识(ftp)
253.
  * @return boolean
254.
  */
255.
 public function chmod($path, $perm) {
256.
  if( ! $this->_isconn()) {
257.
   return FALSE;
258.
  }
259.
  //只有在PHP5中才定义了修改权限的函数(ftp)
260.
  if( ! function_exists('ftp_chmod')) {
261.
   if($this->debug === TRUE) {
262.
    $this->_error("ftp_unable_to_chmod(function)");
263.
   }
264.
   return FALSE;
265.
  }
266.
  $result = @ftp_chmod($this->conn_id, $perm, $path);
267.
  if($result === FALSE) {
268.
   if($this->debug === TRUE) {
269.
    $this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]");
270.
   }
271.
   return FALSE;
272.
  }
273.
  return TRUE;
274.
 }
275.
 /**
276.
  * 获取目录文件列表
277.
  *
278.
  * @access  public
279.
  * @param  string  目录标识(ftp)
280.
  * @return array
281.
  */
282.
 public function filelist($path = '.') {
283.
  if( ! $this->_isconn()) {
284.
   return FALSE;
285.
  }
286.
  return ftp_nlist($this->conn_id, $path);
287.
 }
288.
 /**
289.
  * 关闭FTP
290.
  *
291.
  * @access  public
292.
  * @return boolean
293.
  */
294.
 public function close() {
295.
  if( ! $this->_isconn()) {
296.
   return FALSE;
297.
  }
298.
  return @ftp_close($this->conn_id);
299.
 }
300.
 /**
301.
  * FTP成员变量初始化
302.
  *
303.
  * @access private
304.
  * @param array 配置数组  
305.
  * @return void
306.
  */
307.
 private function _init($config = array()) {
308.
  foreach($config as $key => $val) {
309.
   if(isset($this->$key)) {
310.
    $this->$key = $val;
311.
   }
312.
  }
313.
  //特殊字符过滤
314.
  $this->hostname = preg_replace('|.+?://|','',$this->hostname);
315.
 }
316.
 /**
317.
  * FTP登陆
318.
  *
319.
  * @access  private
320.
  * @return boolean
321.
  */
322.
 private function _login() {
323.
  return @ftp_login($this->conn_id, $this->username, $this->password);
324.
 }
325.
 /**
326.
  * 判断con_id
327.
  *
328.
  * @access  private
329.
  * @return boolean
330.
  */
331.
 private function _isconn() {
332.
  if( ! is_resource($this->conn_id)) {
333.
   if($this->debug === TRUE) {
334.
    $this->_error("ftp_no_connection");
335.
   }
336.
   return FALSE;
337.
  }
338.
  return TRUE;
339.
 }
340.
 /**
341.
  * 从文件名中获取后缀扩展
342.
  *
343.
  * @access  private
344.
  * @param  string  目录标识
345.
  * @return string
346.
  */
347.
 private function _getext($filename) {
348.
  if(FALSE === strpos($filename, '.')) {
349.
   return 'txt';
350.
  }
351.
  $extarr = explode('.', $filename);
352.
  return end($extarr);
353.
 }
354.
 /**
355.
  * 从后缀扩展定义FTP传输模式  ascii 或 binary
356.
  *
357.
  * @access  private
358.
  * @param  string  后缀扩展
359.
  * @return string
360.
  */
361.
 private function _settype($ext) {
362.
  $text_type = array (
363.
       'txt',
364.
       'text',
365.
       'php',
366.
       'phps',
367.
       'php4',
368.
       'js',
369.
       'css',
370.
       'htm',
371.
       'html',
372.
       'phtml',
373.
       'shtml',
374.
       'log',
375.
       'xml'
376.
       );
377.
  return (in_array($ext, $text_type)) ? 'ascii' : 'binary';
378.
 }
379.
 /**
380.
  * 错误日志记录
381.
  *
382.
  * @access  prvate
383.
  * @return boolean
384.
  */
385.
 private function _error($msg) {
386.
  return @file_put_contents('ftp_err.log', "date[".date("Y-m-d H:i:s")."]-hostname[".$this->hostname."]-username[".$this->username."]-password[".$this->password."]-msg[".$msg."]\n", FILE_APPEND);
387.
 }
388.
}
389.
/*End of file ftp.php*/
390.
/*Location /Apache Group/htdocs/ftp.php*/

DEMO 

01.
<?php
02.
require_once('ftp.php');
03.
$config = array(
04.
   'hostname' => 'localhost',
05.
   'username' => 'root',
06.
   'password' => 'root',
07.
   'port' => 21
08.
    );
09.
$ftp = new Ftp();
10.
$ftp->connect($config);
11.
$ftp->upload('ftp_err.log','ftp_upload.log');
12.
$ftp->download('ftp_upload.log','ftp_download.log');
13.
/*End of file ftp_demo.php*/
14.
/*Location: /htdocs/ftp_demo.php*/


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.sulao.cn/post/458

相关推荐