PHP中FTP模块的一些应用

作者 : admin 于 2008年08月27日, 10:20:06
2008
08-27
  1. <?php
  2. class ftp
  3. {
  4. //connect id
  5. var $ftp;
  6. //remote dir
  7. var $remotedir = '/';
  8. //local dir
  9. var $localdir = './';
  10. //write log
  11. var $writelog = false;
  12. //log file
  13. var $logfile = 'ftp_log.txt';
  14. //print log
  15. var $printlog = false;
  16. //session time
  17. var $timeout = 60;
  18. //construct connetc
  19. function __construct( $host , $user , $pass )
  20. {
  21. $this->ftp = @ftp_connect( $host );
  22. ftp_login( $this->ftp, $user , $pass );
  23. //ftp_set_option( $this->ftp , FTP_TIMEOUT_SEC , $this->timeout );
  24. }
  25. //list sub files
  26. function __list( $dir = false , $subtree = false )
  27. {
  28. $rlist = array();
  29. if(!$dir)
  30. {
  31. $dir = $this->remotedir;
  32. }
  33. $list = ftp_rawlist($this->ftp,$dir,TRUE);
  34. if(is_array($list))
  35. {
  36. foreach( $list as $key => $value )
  37. {
  38. $tmp = explode( ' ' , $value );
  39. $rlist[$key] = end($tmp);
  40. if(@ftp_chdir($this->ftp,$dir.$rlist[$key].'/'))
  41. {
  42. $rlist[$rlist[$key]] = $this->__list( $dir.$rlist[$key].'/' );
  43. unset($rlist[$key]);
  44. }
  45. unset($tmp);
  46. }
  47. }
  48. return $rlist;
  49. }
  50. //change dir  $isabs = false,use rela dir default
  51. function __chdir( $dir , $isabs = false )
  52. {
  53. if(!$isabs)
  54. {
  55. $nextdir = $this->remotedir.$dir.'/';
  56. }
  57. else
  58. {
  59. $nextdir = $dir;
  60. }
  61. if(ftp_chdir($this->ftp,$nextdir))
  62. {
  63. $this->remotedir = $nextdir;
  64. return true;
  65. }
  66. else
  67. {
  68. return false;
  69. }
  70. }
  71. //close connect
  72. function __close()
  73. {
  74. @ftp_close($this->ftp);
  75. }
  76. }
  77. $ftp = new ftp( 'www.sunboyu.cn' , '**', '***' );
  78. $ftp->__chdir('website',0);
  79. $list = $ftp->__list('/');
  80. echo $a = time();
  81. print_r($list);
  82. echo time() - $a;
  83. ?>