PHP Download remote File Class (support breakpoint continuation)

Source: Internet
Author: User

PHP Downloads A remote file class that supports a breakpoint continuation download, and the code contains specific invocation instructions. The program is mainly using the HTTP protocol to download files, HTTP1.1 protocol must specify the end of the document to close the link, otherwise read the document can not use the feof judgment end, there are two ways to use, specific download view source.

<?php/** * Download remote file class support breakpoint Extension/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 ($urls ["Pass"]) $this->m_pass = $urls [!empty];
		    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 ();
		/** * Get the reason for an operation error/Public Function Printerror () {echo error message:. $this->m_error;
		echo "Concrete return head:<br>"; 
		foreach ($this->m_httphead as $k => $v) {echo "$k => $v <br>rn";  
			}/** * Discriminant The answer result 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 returned page is a text type/Public function Istext () {if (Ereg ("^2", $this->gethead ("Http-state")) &&amp ; 
		Eregi ("^text", $this->gethead ("Content-type"))) {return true;
			else {$this->m_error. = "Content is not text type <br>";
		return false;  }/** * Determines whether the returned Web page is 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 file with 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 Web page contents as text file */Public Function Savetotext ($savefilename) {if ($this->istext ()) {$this->save
		Binfile ($savefilename);
		else {return "";
		}/** * uses HTTP protocol to obtain the content of a Web page/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_e
			Rror. = "Open remote host error!";
		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. "$httpvrn"); Fputs ($this->m_fp, "Host:". $this->m_host. "
		RN "); Fputs ($this->m_fp, "AccEPT: */*rn ");
		Fputs ($this->m_fp, "user-agent:mozilla/4.0+ (compatible;+msie+6.0;+windows+nt+5.2) rn"); #HTTP1.1 protocol must specify that the link be closed after the end of the document, otherwise you cannot use feof to judge the End if ($HTTPV = = "http/1.1") {fputs ($this->m_fp, "Connection:closernrn")
		;
		else {fputs ($this->m_fp, "RN");
		} $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;
	}/** * Shut down connection/public function close () {@fclose ($this->m_fp);
The #两种使用方法, respectively, are 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 filename $file->close (); # Free Resources?>
 

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.