Supports remote file download for resumable PHP

Source: Internet
Author: User
Tags ereg
Supports remote file download for resumable PHP

PHP remote file download class, supports resumable download, the code contains specific call instructions. The program uses the HTTP protocol to download files. the HTTP1.1 protocol must specify that the link is closed after the document ends. otherwise, feof cannot be used to judge the end of the document. you can use either of the following methods, for details, download and view the source code.

  1. /**
  2. * Supports resumable Upload for remote file downloads.
  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. # Resetting parameters
  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. # Initialize the system
  58. $ This-> PrivateInit ($ url );
  59. $ This-> PrivateStartSession ();
  60. }
  61. /**
  62. * Obtain the cause of an operation error.
  63. */
  64. Public function printError (){
  65. Echo "error message:". $ this-> m_error;
  66. Echo "specific return headers:
    ";
  67. Foreach ($ this-> m_httphead as $ k => $ v ){
  68. Echo "$ k => $ v
    \ R \ n ";
  69. }
  70. }
  71. /**
  72. * Determine whether the response results of the headers sent using the Get method are 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. * Check whether the returned webpage is of the text type.
  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. = "The content is non-text
    ";
  90. Return false;
  91. }
  92. }
  93. /**
  94. * Determine whether the returned webpage 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. = "The type is incorrect". $ this-> GetHead ("content-type ")."
    ";
  101. Return false;
  102. }
  103. }
  104. /**
  105. * Download an object over HTTP
  106. */
  107. Public function SaveToBin ($ savefilename ){
  108. If (! $ This-> IsGetOK () return false;
  109. If (@ feof ($ this-> m_fp )){
  110. $ This-> m_error = "The connection has been closed! ";
  111. Return false;
  112. }
  113. $ Fp = fopen ($ savefilename, "w") or die ("failed to write the file $ 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. * Save the webpage content as a Text file
  122. */
  123. Public function SaveToText ($ savefilename ){
  124. If ($ this-> IsText ()){
  125. $ This-> SaveBinFile ($ savefilename );
  126. } Else {
  127. Return "";
  128. }
  129. }
  130. /**
  131. * Use HTTP to obtain the content of a webpage
  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 an HTTP session
  145. */
  146. Public function PrivateStartSession (){
  147. If (! $ This-> PrivateOpenHost ()){
  148. $ This-> m_error. = "an error occurred while opening the 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. # In HTTP1.1, the link must be closed after the document ends. otherwise, feof cannot be used to judge the end of the document.
  161. If ($ httpv = "HTTP/1.1 "){
  162. Fputs ($ this-> m_fp, "Connection: Close \ 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. * Get the value of an Http header
  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. * Open the connection.
  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. * Close the connection.
  208. */
  209. Public function Close (){
  210. @ Fclose ($ this-> m_fp );
  211. }
  212. }
  213. # Use the following methods:
  214. # Open a webpage
  215. $ Httpdown = new HttpDownload ();
  216. $ Httpdown-> OpenUrl ("http://www.google.com.hk ");
  217. Echo $ httpdown-> GetHtml ();
  218. $ Httpdown-> Close ();
  219. # Download an object
  220. $ File = new HttpDownload (); # instantiate a class
  221. $ File-> OpenUrl ("http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # remote file address
  222. $ File-> SaveToBin ("rust020133"); # save path and file name
  223. $ File-> Close (); # release resources
  224. ?>

Resumable Upload, 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.