Ftp.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\common\util;
  3. //$config = array('ftp_host'=>'xxx.xxx.xxx.xxx','ftp_port'=>21,'ftp_user'=>'xxx','ftp_pwd' =>'xxxxx','ftp_dir'=>'/');
  4. //$ftp=new ftp();
  5. //$ftp->config($config);
  6. //$ftp->connect();
  7. //$ftp->put(ROOT_PATH.$file, $file);
  8. //@unlink($this->getPicFile($file));
  9. class Ftp{
  10. protected $_config = array( 'ftp_host'=>'www.test.com', 'ftp_port'=>'21', 'ftp_user'=>'maccms', 'ftp_pwd' =>'maccms', 'ftp_timeout'=>'30', 'ftp_dir' =>'/', 'ftp_pasv'=>1 );
  11. protected $_conn = null;
  12. protected $_rs = null;
  13. public function __construct($config=array()){
  14. !function_exists('ftp_connect') && die('FTP模块不支持!');
  15. $this->config($config);
  16. }
  17. public function config($config=array()){
  18. $this->_config = array_merge($this->_config, $config);
  19. }
  20. function connect(){
  21. $this->_conn = @ftp_connect($this->_config['ftp_host'],$this->_config['ftp_port'],$this->_config['ftp_timeout']);
  22. if(!$this->_conn){
  23. return -1;//FTP服务器连接失败! 请检查服务器地址和端口
  24. }
  25. $this->_rs = @ftp_login($this->_conn, $this->_config['ftp_user'], $this->_config['ftp_pwd']);
  26. if(!$this->_rs){
  27. return -2; //FTP登录错误! 请检查用户名和密码
  28. }
  29. $this->_config['ftp_pasv'] && $this->pasv(true);
  30. if(!$this->chdir($this->_config['ftp_dir'])){
  31. return -3; //切换到FTP当前目录失败! 请检查目录是否存在
  32. }
  33. return $this;
  34. }
  35. function chdir($dir){
  36. return @ftp_chdir($this->_conn,$dir);
  37. }
  38. function is_file($file){
  39. $buff = @ftp_mdtm($this->_conn, $file);
  40. if($buff != -1){
  41. return true;
  42. }else{
  43. return false;
  44. }
  45. }
  46. function pasv($mode=true){
  47. return @ftp_pasv($this->_conn, true);
  48. }
  49. function put($local_file, $remote_file, $mode='B'){
  50. if($mode == 'B'){
  51. $mode = FTP_BINARY;
  52. }else{
  53. $mode = FTP_ASCII;
  54. }
  55. $this->mkdirs(dirname($remote_file));
  56. $rs = @ftp_put($this->_conn, $remote_file, $local_file, $mode);
  57. return $rs;
  58. }
  59. function mkdirs($dir){
  60. $dir = str_replace("\\",'/',$dir);
  61. $dirs = explode('/', $dir);
  62. $total = count($dirs);
  63. foreach($dirs as $val){
  64. if($val == '.'){
  65. continue;
  66. }
  67. if($this->chdir($val) == false){
  68. if(!$this->mkdir($val)){
  69. return false;//创建失败
  70. }
  71. $this->chdir($val);
  72. }
  73. }
  74. $this->chdir($this->_config['ftp_dir']);
  75. return true;
  76. }
  77. function mkdir($dir){
  78. return @ftp_mkdir($this->_conn, $dir);
  79. }
  80. function unlink($file){
  81. return @ftp_delete($this->_conn, $file);
  82. }
  83. function rename($old_name, $new_name){
  84. return @ftp_rename($this->_conn, $old_name, $new_name);
  85. }
  86. function rmdir($dir){
  87. return @ftp_rmdir($this->_conn, $dir);
  88. }
  89. function rmdirs($dir, $flag=1){
  90. $res = $this->rmdir($dir) || $this->unlink($dir);
  91. if(!$res){
  92. $files = $this->nlist($dir);
  93. if(empty($files)){
  94. return true;
  95. }
  96. foreach($files as $file){
  97. $file = basename($file);
  98. $this->rmdirs($dir.'/'.$file);
  99. }
  100. if($flag){
  101. $this->rmdirs($dir);
  102. }
  103. }
  104. return true;
  105. }
  106. function nlist($dir){
  107. return @ftp_nlist($this->_conn, $dir);
  108. }
  109. function bye() {
  110. return ftp_close($this->_conn);
  111. }
  112. }
  113. ?>