欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

支持断点续传的PHP 下载远程文件类

程序员文章站 2022-05-16 17:17:58
...

PHP 下载远程文件类,支持断点续传下载,代码内含有具体的调用说明。程序主要是使用 HTTP 协议下载文件,HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束,可以有两种使用方法,具体请下载查看源码。

  1. /**
  2. * 下载远程文件类支持断点续传
  3. */
  4. class HttpDownload {
  5. private $m_url = "";
  6. private $m_urlpath = "";
  7. private $m_scheme = "http";
  8. private $m_host = "";
  9. private $m_port = "80";
  10. private $m_user = "";
  11. private $m_pass = "";
  12. private $m_path = "/";
  13. private $m_query = "";
  14. private $m_fp = "";
  15. private $m_error = "";
  16. private $m_httphead = "" ;
  17. private $m_html = "";
  18. /**
  19. * 初始化
  20. */
  21. public function PrivateInit($url){
  22. $urls = "";
  23. $urls = @parse_url($url);
  24. $this->m_url = $url;
  25. if(is_array($urls)) {
  26. $this->m_host = $urls["host"];
  27. if(!empty($urls["scheme"])) $this->m_scheme = $urls["scheme"];
  28. if(!empty($urls["user"])) $this->m_user = $urls["user"];
  29. if(!empty($urls["pass"])) $this->m_pass = $urls["pass"];
  30. if(!empty($urls["port"])) $this->m_port = $urls["port"];
  31. if(!empty($urls["path"])) $this->m_path = $urls["path"];
  32. $this->m_urlpath = $this->m_path;
  33. if(!empty($urls["query"])) {
  34. $this->m_query = $urls["query"];
  35. $this->m_urlpath .= "?".$this->m_query;
  36. }
  37. }
  38. }
  39. /**
  40. * 打开指定网址
  41. */
  42. function OpenUrl($url) {
  43. #重设各参数
  44. $this->m_url = "";
  45. $this->m_urlpath = "";
  46. $this->m_scheme = "http";
  47. $this->m_host = "";
  48. $this->m_port = "80";
  49. $this->m_user = "";
  50. $this->m_pass = "";
  51. $this->m_path = "/";
  52. $this->m_query = "";
  53. $this->m_error = "";
  54. $this->m_httphead = "" ;
  55. $this->m_html = "";
  56. $this->Close();
  57. #初始化系统
  58. $this->PrivateInit($url);
  59. $this->PrivateStartSession();
  60. }
  61. /**
  62. * 获得某操作错误的原因
  63. */
  64. public function printError() {
  65. echo "错误信息:".$this->m_error;
  66. echo "具体返回头:
    ";
  67. foreach($this->m_httphead as $k=>$v) {
  68. echo "$k => $v
    \r\n";
  69. }
  70. }
  71. /**
  72. * 判别用Get方法发送的头的应答结果是否正确
  73. */
  74. public function IsGetOK() {
  75. if( ereg("^2",$this->GetHead("http-state")) ) {
  76. return true;
  77. } else {
  78. $this->m_error .= $this->GetHead("http-state")." - ".$this->GetHead("http-describe")."
    ";
  79. return false;
  80. }
  81. }
  82. /**
  83. * 看看返回的网页是否是text类型
  84. */
  85. public function IsText() {
  86. if (ereg("^2",$this->GetHead("http-state")) && eregi("^text",$this->GetHead("content-type"))) {
  87. return true;
  88. } else {
  89. $this->m_error .= "内容为非文本类型
    ";
  90. return false;
  91. }
  92. }
  93. /**
  94. * 判断返回的网页是否是特定的类型
  95. */
  96. public function IsContentType($ctype) {
  97. if (ereg("^2",$this->GetHead("http-state")) && $this->GetHead("content-type") == strtolower($ctype)) {
  98. return true;
  99. } else {
  100. $this->m_error .= "类型不对 ".$this->GetHead("content-type")."
    ";
  101. return false;
  102. }
  103. }
  104. /**
  105. * 用 HTTP 协议下载文件
  106. */
  107. public function SaveToBin($savefilename) {
  108. if (!$this->IsGetOK()) return false;
  109. if (@feof($this->m_fp)) {
  110. $this->m_error = "连接已经关闭!";
  111. return false;
  112. }
  113. $fp = fopen($savefilename,"w") or die("写入文件 $savefilename 失败!");
  114. while (!feof($this->m_fp)) {
  115. @fwrite($fp,fgets($this->m_fp,256));
  116. }
  117. @fclose($this->m_fp);
  118. return true;
  119. }
  120. /**
  121. * 保存网页内容为 Text 文件
  122. */
  123. public function SaveToText($savefilename) {
  124. if ($this->IsText()) {
  125. $this->SaveBinFile($savefilename);
  126. } else {
  127. return "";
  128. }
  129. }
  130. /**
  131. * 用 HTTP 协议获得一个网页的内容
  132. */
  133. public function GetHtml() {
  134. if (!$this->IsText()) return "";
  135. if ($this->m_html!="") return $this->m_html;
  136. if (!$this->m_fp||@feof($this->m_fp)) return "";
  137. while(!feof($this->m_fp)) {
  138. $this->m_html .= fgets($this->m_fp,256);
  139. }
  140. @fclose($this->m_fp);
  141. return $this->m_html;
  142. }
  143. /**
  144. * 开始 HTTP 会话
  145. */
  146. public function PrivateStartSession() {
  147. if (!$this->PrivateOpenHost()) {
  148. $this->m_error .= "打开远程主机出错!";
  149. return false;
  150. }
  151. if ($this->GetHead("http-edition")=="HTTP/1.1") {
  152. $httpv = "HTTP/1.1";
  153. } else {
  154. $httpv = "HTTP/1.0";
  155. }
  156. fputs($this->m_fp,"GET ".$this->m_urlpath." $httpv\r\n");
  157. fputs($this->m_fp,"Host: ".$this->m_host."\r\n");
  158. fputs($this->m_fp,"Accept: */*\r\n");
  159. fputs($this->m_fp,"User-Agent: Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2)\r\n");
  160. #HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束
  161. if ($httpv=="HTTP/1.1") {
  162. fputs($this->m_fp,"Connection: Close\r\n\r\n");
  163. } else {
  164. fputs($this->m_fp,"\r\n");
  165. }
  166. $httpstas = fgets($this->m_fp,256);
  167. $httpstas = split(" ",$httpstas);
  168. $this->m_httphead["http-edition"] = trim($httpstas[0]);
  169. $this->m_httphead["http-state"] = trim($httpstas[1]);
  170. $this->m_httphead["http-describe"] = "";
  171. for ($i=2;$i $this->m_httphead["http-describe"] .= " ".trim($httpstas[$i]);
  172. }
  173. while (!feof($this->m_fp)) {
  174. $line = str_replace("\"","",trim(fgets($this->m_fp,256)));
  175. if($line == "") break;
  176. if (ereg(":",$line)) {
  177. $lines = split(":",$line);
  178. $this->m_httphead[strtolower(trim($lines[0]))] = trim($lines[1]);
  179. }
  180. }
  181. }
  182. /**
  183. * 获得一个Http头的值
  184. */
  185. public function GetHead($headname) {
  186. $headname = strtolower($headname);
  187. if (isset($this->m_httphead[$headname])) {
  188. return $this->m_httphead[$headname];
  189. } else {
  190. return "";
  191. }
  192. }
  193. /**
  194. * 打开连接
  195. */
  196. public function PrivateOpenHost() {
  197. if ($this->m_host=="") return false;
  198. $this->m_fp = @fsockopen($this->m_host, $this->m_port, &$errno, &$errstr,10);
  199. if (!$this->m_fp){
  200. $this->m_error = $errstr;
  201. return false;
  202. } else {
  203. return true;
  204. }
  205. }
  206. /**
  207. * 关闭连接
  208. */
  209. public function Close(){
  210. @fclose($this->m_fp);
  211. }
  212. }
  213. #两种使用方法,分别如下:
  214. #打开网页
  215. $httpdown = new HttpDownload();
  216. $httpdown->OpenUrl("http://www.google.com.hk");
  217. echo $httpdown->GetHtml();
  218. $httpdown->Close();
  219. #下载文件
  220. $file = new HttpDownload(); # 实例化类
  221. $file->OpenUrl("http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # 远程文件地址
  222. $file->SaveToBin("rust020.pdf"); # 保存路径及文件名
  223. $file->Close(); # 释放资源
  224. ?>
复制代码

断点续传, PHP