This article mainly introduces the PHP implementation of the download remote file class definition and usage, combined with specific examples of the download of PHP package remote file Operation class definition and use method, the need for friends can refer to the following
Specific as follows:
<?php/** * Download remote file class support breakpoint continuation */class httpdownload {private $m _url = ""; Private $m _urlpath = ""; Private $m _scheme = "http"; Private $m _host = ""; Private $m _port = "80"; Private $m _user = ""; Private $m _pass = ""; Private $m _path = "/"; Private $m _query = ""; Private $m _FP = ""; Private $m _error = ""; Private $m _httphead = ""; Private $m _html = ""; /** * Initialize * */Public Function Privateinit ($url) {$urls = ""; $urls = @parse_url ($url); $this->m_url = $url; if (Is_array ($urls)) {$this->m_host = $urls ["Host"]; if (!empty ($urls ["scheme"]) $this->m_scheme = $urls ["scheme"]; if (!empty ($urls ["User"]) $this->m_user = $urls ["User"]; if (!empty ($urls ["Pass"]) $this->m_pass = $urls ["Pass"]; if (!empty ($urls ["Port"]) $this->m_port = $urls ["Port"]; if (!empty ($urls ["path"])) $this->m_path = $urls ["Path"]; $this->m_urlpath = $this->m_path; if (!empty ($urls ["Query"])) {$this->m_query = $urls ["qUery "]; $this->m_urlpath. = "?". $this->m_query; }}}/** * Opens the specified URL */function OpenUrl ($url) {#重设各参数 $this->m_url = ""; $this->m_urlpath = ""; $this->m_scheme = "http"; $this->m_host = ""; $this->m_port = "80"; $this->m_user = ""; $this->m_pass = ""; $this->m_path = "/"; $this->m_query = ""; $this->m_error = ""; $this->m_httphead = ""; $this->m_html = ""; $this->close (); #初始化系统 $this->privateinit ($url); $this->privatestartsession (); }/** * The reason for getting an operation error */Public function Printerror () {echo "error message:". $this->m_error; echo "Specific return head:<br>"; foreach ($this->m_httphead as $k + $v) {echo "$k + $v <br>\r\n"; }}/** * discriminant the answer of the header sent with the Get method is correct */Public function Isgetok () {if (Ereg ("^2", $this->gethead ("Http-state")) { return true; } else {$this->m_error. = $this->gethead ("Http-state"). "-". $this->gEthead ("Http-describe"). " <br> "; return false; }}/** * see if the page returned is text type */Public function Istext () {if (Ereg ("^2", $this->gethead ("Http-state") && Eregi ("^text", $this->gethead ("Content-type"))) {return true; } else {$this->m_error. = "Content is non-text type <br>"; return false; }}/** * Determines whether the returned page is of a specific type */Public function Iscontenttype ($ctype) {if (Ereg ("^2", $this->gethead ("Http-state") ) && $this->gethead ("content-type") = = Strtolower ($ctype)) {return true; } else {$this->m_error. = "wrong type". $this->gethead ("Content-type"). " <br> "; return false; }}/** * Download the file with the HTTP protocol */Public function Savetobin ($savefilename) {if (! $this->isgetok ()) return false; if (@feof ($this->m_fp)) {$this->m_error = "The connection is closed! "; return false; } $fp = fopen ($savefilename, "w") or Die ("Write File $savefilename failed! "); while (!feof ($this->m_fp)) {@fwrite ($fp, Fgets ($this->m_fp,256)); } @fclose ($this->m_fp); return true; /** * Save page content as Text file */Public Function Savetotext ($savefilename) {if ($this->istext ()) {$this->sav Ebinfile ($savefilename); } else {return ""; }}/** * Get the content of a Web page with the HTTP protocol */Public function gethtml () {if (! $this->istext ()) return ""; if ($this->m_html!= "") return $this->m_html; if (! $this->m_fp| | @feof ($this->m_fp)) return ""; while (!feof ($this->m_fp)) {$this->m_html. = fgets ($this->m_fp,256); } @fclose ($this->m_fp); return $this->m_html; }/** * Start HTTP session */Public function privatestartsession () {if (! $this->privateopenhost ()) {$this->m_ Error. = "Error opening remote host!"; return false; } if ($this->gethead ("http-edition") = = "http/1.1") {$HTTPV = "http/1.1"; } else {$HTTPV = "http/1.0"; } fputs ($this->m_fp, "GET". $this->m_urlpath. "$HTTPV \ r \ n"); Fputs ($this->m_fp, "HoST: ". $this->m_host." \ r \ n "); Fputs ($this->m_fp, "Accept: */*\r\n"); Fputs ($this->m_fp, "user-agent:mozilla/4.0+ (compatible;+msie+6.0;+windows+nt+5.2) \ r \ n"); #HTTP1.1 The protocol must close the link after the document is closed, otherwise you cannot use feof to determine the end if ($HTTPV = = "http/1.1") {fputs ($this->m_fp, "connection:close\r\n\") when reading the document. r\n "); } else {fputs ($this->m_fp, "\ r \ n"); } $httpstas = fgets ($this->m_fp,256); $httpstas = Split ("", $httpstas); $this->m_httphead["http-edition") = Trim ($httpstas [0]); $this->m_httphead["http-state") = Trim ($httpstas [1]); $this->m_httphead["http-describe"] = ""; for ($i =2; $i <count ($httpstas); $i + +) {$this->m_httphead["http-describe"]. Trim ($httpstas [$i]); } while (!feof ($this->m_fp)) {$line = Str_replace ("\" "," ", Trim (fgets ($this->m_fp,256))); if ($line = = "") break; if (Ereg (":", $line)) {$lines = Split (":", $line); $this->m_httphead[strtolower (Trim ($lines [0])] = Trim ($lines [1]); } }}/** * Gets the value of an HTTP header */Public Function GetHead ($headname) {$headname = Strtolower ($headname); if (Isset ($this->m_httphead[$headname]) {return $this->m_httphead[$headname]; } else {return ""; }}/** * Open connection */Public Function privateopenhost () {if ($this->m_host== "") return false; $this->m_fp = @fsockopen ($this->m_host, $this->m_port, & $errno, & $errstr, 10); if (! $this->m_fp) {$this->m_error = $errstr; return false; } else {return true; }}/** * Close connection */Public function close () {@fclose ($this->m_fp); }} #两种使用方法, as follows: #打开网页 $httpdown = new Httpdownload (); $httpdown->openurl ("http://www.google.com.hk"); echo $ Httpdown->gethtml (); $httpdown->close (); #下载文件 $file = new Httpdownload (); # Instantiate class $file->openurl ("Http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # Remote File Address $file->savetobin ("rust020.pdf"); # Save path and file name $file->close (); # Release Resources?>
Related recommendations:
PHP implementation Download remote file class related code
PHP Download remote file store to local development sample introduction
PHP Download remote file class share