class_ftp.php


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

php的FTP操作类的更多相关文章

  1. PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )

    /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...

  2. C# FTP操作类的代码

    如下代码是关于C# FTP操作类的代码.using System;using System.Collections.Generic;using System.Text;using System.Net ...

  3. FTP操作类

    using System; using System.Collections.Generic; using System.Net; using System.IO; namespace HGFTP { ...

  4. 很实用的FTP操作类

    using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; using ...

  5. FTP操作类的使用

    FTP(文件传输协议) FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序 ...

  6. (转)FTP操作类,从FTP下载文件

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...

  7. FTP操作类(支持异步)

    public delegate void DownloadProgressChangedEventHandle(string information, long currentprogress, lo ...

  8. c#FTP操作类,包含上传,下载,删除,获取FTP文件列表文件夹等Hhelp类

    有些时间没发表文章了,之前用到过,这是我总结出来关于ftp相关操作一些方法,网上也有很多,但是没有那么全面,我的这些仅供参考和借鉴,希望能够帮助到大家,代码和相关引用我都复制粘贴出来了,希望大家喜欢 ...

  9. [转]C# FTP操作类

      转自 http://www.cnblogs.com/Liyuting/p/7084718.html using System; using System.Collections.Generic; ...

随机推荐

  1. UOJ450 复读机

    题意:n个位置,k种颜色.求有多少种方案使得每种颜色恰出现d的倍数次. 解:d=1就快速幂,n,k很小就DP,记得乘组合数来分配位置. d = 2 / 3的时候,考虑生成函数. f(x) = ∑[d ...

  2. 07.27NOIP模拟赛

    戳这里下载过去三次NOIP模拟赛总成绩 (别嘲笑垫底的我...解压密码为信奥生所在的两个班的班号,文档密码为机房开机用户名+密码) 又一次垫底…… 我难受. 上来感觉T1不可做,T2和蔼可亲,T3一脸 ...

  3. python dict 实现swich

    使用dict实现swich,通过不同的键映射不同的函数. swich = { 'hour1':pred.getper1htable, 'hour6':pred.getper6htable, 'hour ...

  4. 05-python 学习第五天-简单验证码

    通过python 随机数可以制作简单的验证码. 1.0版本来了,这验证码,只有一个码,功能虽然达不到,逻辑还是准确的,目前还不能算是验证码,但是我们会继续完善的. import random # 导入 ...

  5. <每日一题>题目17:super()继承相关的面试题

    class A(object): def go(self): print("go A go!") def stop(self): print("stop A stop!& ...

  6. MyEclipse 最常用实用快捷键

  7. 初识莫队——小Z的袜子

    以前一直觉得莫队是多么高大上的一种算法,然而仔细看了下发现其实并不复杂,实质上就是技巧性的暴力美学. 在我看来莫队是一种分块排序后降低复杂度的算法,当答案可以通过左右端点一个一个移动维护出来的时候就可 ...

  8. THUWC 游记

    考试前的一个周末 PKUWC没过,去不了,自闭,我死了. 考试前的星期一 THUWC居然过了!!!大恩大德永世难忘,我又活了. 考试前的周四 WTF!??为什么要用Ubuntu,我完全不会,凉了凉了, ...

  9. Leetcode961. N-Repeated Element in Size 2N Array重复N次的元素

    在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次. 返回重复了 N 次的那个元素. 示例 1: 输入:[1,2,3,3] 输出:3 示例 2: 输入:[2,1,2, ...

  10. 微信小程序之组件的集合(五)

    这个是学习复杂的组件的封装的,在课程中,主要实现的是书单上方的搜索功能组件的开发,这个应该是较之前的组件是有一定难度的,但是现在学到现在,感觉前端的内容和后端的内容比较起来,还是比较容易的,而且好多内 ...