PHP ftp上传文件操作类的完整代码

前端之家收集整理的这篇文章主要介绍了PHP ftp上传文件操作类的完整代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣PHP ftp上传文件操作类的完整代码的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。<br>
  1. /**
  2. * @param
  3. * @author 编程之家 jb51.cc jb51.cc
  4. * 作用:FTP操作类( 拷贝、移动、删除文件/创建目录 )
  5. */
  6. class class_ftp
  7. {
  8. public $off; // 返回操作状态(成功/失败)
  9. public $conn_id; // FTP连接
  10. const FTP_HOST='*.*.*.*';
  11. const FTP_PORT='21';
  12. const FTP_USER='*******';
  13. const FTP_PASS='*******';
  14. /**
  15. * 方法:FTP连接
  16. * @FTP_HOST -- FTP主机
  17. * @FTP_PORT -- 端口
  18. * @FTP_USER -- 用户名
  19. * @FTP_PASS -- 密码
  20. */
  21. function __construct()
  22. {
  23. $this->conn_id = @ftp_connect(self::FTP_HOST,self::FTP_PORT) or die("FTP服务器连接失败");
  24. @ftp_login($this->conn_id,self::FTP_USER,self::FTP_PASS) or die("FTP服务器登陆失败");
  25. @ftp_pasv($this->conn_id,1); // 打开被动模拟
  26. }
  27. /**
  28. * 方法上传文件
  29. * @path -- 本地路径
  30. * @newpath -- 上传路径
  31. * @type -- 若目标目录不存在则新建
  32. */
  33. function up_file($path,$newpath,$type=true)
  34. {
  35. var_dump($this->conn_id);exit;
  36. if($type) $this->dir_mkdirs($newpath);
  37. $this->off = @ftp_put($this->conn_id,$path,FTP_BINARY);
  38. if(!$this->off) echo "文件上传失败,请检查权限及路径是否正确!";
  39. }
  40. /**
  41. * 方法:移动文件
  42. * @path -- 原路径
  43. * @newpath -- 新路径
  44. * @type -- 若目标目录不存在则新建
  45. */
  46. function move_file($path,$type=true)
  47. {
  48. if($type) $this->dir_mkdirs($newpath);
  49. $this->off = @ftp_rename($this->conn_id,$newpath);
  50. if(!$this->off) echo "文件移动失败,请检查权限及原路径是否正确!";
  51. }
  52. /**
  53. * 方法:复制文件
  54. * 说明:由于FTP无复制命令,本方法变通操作为:下载后再上传到新的路径
  55. * @path -- 原路径
  56. * @newpath -- 新路径
  57. * @type -- 若目标目录不存在则新建
  58. */
  59. function copy_file($path,$type=true)
  60. {
  61. $downpath = "c:/tmp.dat";
  62. $this->off = @ftp_get($this->conn_id,$downpath,FTP_BINARY);// 下载
  63. if(!$this->off) echo "文件复制失败,请检查权限及原路径是否正确!";
  64. $this->up_file($downpath,$type);
  65. }
  66. /**
  67. * 方法删除文件
  68. * @path -- 路径
  69. */
  70. function del_file($path)
  71. {
  72. $this->off = @ftp_delete($this->conn_id,$path);
  73. if(!$this->off) echo "文件删除失败,请检查权限及路径是否正确!";
  74. }
  75. /**
  76. * 方法生成目录
  77. * @path -- 路径
  78. */
  79. function dir_mkdirs($path)
  80. {
  81. $path_arr = explode('/',$path); // 取目录数组
  82. $file_name = array_pop($path_arr); // 弹出文件
  83. $path_div = count($path_arr); // 取层数
  84. foreach($path_arr as $val) // 创建目录
  85. {
  86. if(@ftp_chdir($this->conn_id,$val) == FALSE)
  87. {
  88. $tmp = @ftp_mkdir($this->conn_id,$val);
  89. if($tmp == FALSE)
  90. {
  91. echo "目录创建失败,请检查权限及路径是否正确!";
  92. exit;
  93. }
  94. @ftp_chdir($this->conn_id,$val);
  95. }
  96. }
  97. for($i=1;$i=$path_div;$i++) // 回退到根
  98. {
  99. @ftp_cdup($this->conn_id);
  100. }
  101. }
  102. /**
  103. * 方法关闭FTP连接
  104. */
  105. function close()
  106. {
  107. @ftp_close($this->conn_id);
  108. }
  109. }// class class_ftp end
  110. /************************************** 测试 ***********************************
  111. $ftp = new class_ftp('192.168.100.143',21,'user','pwd'); // 打开FTP连接
  112. //$ftp->up_file('aa.txt','a/b/c/cc.txt'); // 上传文件
  113. //$ftp->move_file('a/b/c/cc.txt','a/cc.txt'); // 移动文件
  114. //$ftp->copy_file('a/cc.txt','a/b/dd.txt'); // 复制文件
  115. //$ftp->del_file('a/b/dd.txt'); // 删除文件
  116. $ftp->close(); // 关闭FTP连接
  117. ******************************************************************************/
 

猜你在找的PHP相关文章