PHP下载远程文件类源码,带详细注释,还支持断点续传

网站经常要用到下载远程图片的功能,有时还要下载.doc文档,所以就用php开发了一个下载远程文件的类,这个类支持断点续传下载,源代码中有具体的注释及使用说明案例.

程序主要是使用 HTTP 协议下载文件,HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束,可以有两种使用方法,具体请下载查看源码,代码如下:

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