PHP download remote file class with support for breakpoint continuation

Source: Internet
Author: User
Tags ereg php download

PHP download remote file class, support for the continuation of a breakpoint download, the code contains specific invocation instructions. The main program is to use the HTTP protocol to download files, HTTP1.1 protocol must be specified after the end of the document closed link, or read the document can not be used feof judgment end, there are two ways to use, specific download view source.

  1. /**
  2. * Download remote file class support breakpoint continuation
  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. * Initialization
  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. * Open the specified URL
  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. * The reason for getting a wrong operation
  63. */
  64. Public Function Printerror () {
  65. echo "error message:". $this->m_error;
  66. echo "Specific return header:
    ";
  67. foreach ($this->m_httphead as $k = + $v) {
  68. echo "$k and $v
    \ r \ n ";
  69. }
  70. }
  71. /**
  72. * Determine if the answer to the header sent with the Get method is correct
  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. * See if the returned page is of type 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. = "Content is non-text type
    ";
  90. return false;
  91. }
  92. }
  93. /**
  94. * Determine if the returned page is of a specific type
  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. = "wrong type". $this->gethead ("Content-type"). "
    ";
  101. return false;
  102. }
  103. }
  104. /**
  105. * Download files with HTTP protocol
  106. */
  107. Public Function Savetobin ($savefilename) {
  108. if (! $this->isgetok ()) return false;
  109. if (@feof ($this->m_fp)) {
  110. $this->m_error = "The connection is closed! ";
  111. return false;
  112. }
  113. $fp = fopen ($savefilename, "w") or Die ("Write File $savefilename failed! ");
  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. * Save Web content as Text file
  122. */
  123. Public Function Savetotext ($savefilename) {
  124. if ($this->istext ()) {
  125. $this->savebinfile ($savefilename);
  126. } else {
  127. Return "";
  128. }
  129. }
  130. /**
  131. * Get the content of a webpage using the HTTP protocol
  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. * Start HTTP session
  145. */
  146. Public Function privatestartsession () {
  147. if (! $this->privateopenhost ()) {
  148. $this->m_error. = "Error opening remote host!";
  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 The protocol must close the link after the document is closed, or the document cannot be read with feof judgment end
  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
  172. $this->m_httphead["Http-describe". "". Trim ($httpstas [$i]);
  173. }
  174. while (!feof ($this->m_fp)) {
  175. $line = Str_replace ("\" "," ", Trim (fgets ($this->m_fp,256)));
  176. if ($line = = "") break;
  177. if (Ereg (":", $line)) {
  178. $lines = Split (":", $line);
  179. $this->m_httphead[strtolower (Trim ($lines [0])] = Trim ($lines [1]);
  180. }
  181. }
  182. }
  183. /**
  184. * Get the value of an HTTP header
  185. */
  186. Public Function GetHead ($headname) {
  187. $headname = Strtolower ($headname);
  188. if (Isset ($this->m_httphead[$headname])) {
  189. return $this->m_httphead[$headname];
  190. } else {
  191. Return "";
  192. }
  193. }
  194. /**
  195. * Open Connection
  196. */
  197. Public Function Privateopenhost () {
  198. if ($this->m_host== "") return false;
  199. $this->m_fp = @fsockopen ($this->m_host, $this->m_port, & $errno, & $errstr, 10);
  200. if (! $this->m_fp) {
  201. $this->m_error = $errstr;
  202. return false;
  203. } else {
  204. return true;
  205. }
  206. }
  207. /**
  208. * Close Connection
  209. */
  210. Public Function Close () {
  211. @fclose ($this->m_fp);
  212. }
  213. }
  214. #两种使用方法, respectively, as follows:
  215. #打开网页
  216. $httpdown = new Httpdownload ();
  217. $httpdown->openurl ("http://www.google.com.hk");
  218. echo $httpdown->gethtml ();
  219. $httpdown->close ();
  220. #下载文件
  221. $file = new Httpdownload (); # Instantiating classes
  222. $file->openurl ("http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # Remote File Address
  223. $file->savetobin ("rust020.pdf"); # Save path and file name
  224. $file->close (); # Freeing Resources
  225. ?>
Copy Code

Breakpoint Continuation, PHP
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.