From the Dream CMS http file download class _php instance

Source: Internet
Author: User
This example describes the HTTP file download class from the Dream cms. Share to everyone for your reference. Specific as follows:

<?php if (!defined (' Dedeinc ')) exit ("Request error!"); * * Weave Dream HTTP Download class * * @version $Id: dedehttpdown.class.php 1 11:42 July 6, 2010 z Tianya $ * @package dedecms.libraries * @copy Right Copyright (c) 2007-2010, Desdev, Inc. * @license http://help.dedecms.com/usersguide/license.html * @link http: www.dedecms.com */@set_time_limit (0); class dedehttpdown{var $m _url = "; var $m _urlpath ="; var $m _scheme = ' http '; v AR $m _host = "; var $m _port = ' 80 '; var $m _user = "; var $m _pass = "; var $m _path = '/'; var $m _query = "; var $m _fp = "; var $m _error = "; var $m _httphead = "; var $m _html = "; var $m _puthead = "; var $BaseUrlPath = '; var $HomeUrl = '; var $reTry = 0; var $JumpCount = 0; /** * Initialize the system * * @access public * @param string $url the address to download * @return String */function Privateinit ($url) {if ($u  rl== ') {return;  } $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;   } $this->homeurl = $urls ["Host"];   $this->baseurlpath = $this->homeurl. $urls ["Path"]; $this->baseurlpath = Preg_replace ("/\/([^\/]*) \. (.   *) $/","/", $this->baseurlpath);  $this->baseurlpath = preg_replace ("/\/$/", "", $this->baseurlpath);  }}/** * Reset Parameters * * @access public * @return void */function Resetany () {$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 = "";} /** * Open the specified URL * * @access public * @param string $url address * @param string $requestType Request type * @return String * * Fun  Ction OpenUrl ($url, $requestType = "GET") {$this->resetany ();  $this->jumpcount = 0;  $this->m_httphead = Array ();  $this->m_html = ";  $this->retry = 0;  $this->close ();  Initialize the system $this->privateinit ($url); $this->privatestartsession ($requestType); }/** * Go to 303 redirect URL * * @access public * @param string $url address * @return String */function Jumpopenurl ($url) {$th  Is->resetany ();  $this->jumpcount++;  $this->m_httphead = Array ();  $this->m_html = "";  $this->close ();  Initialize the system $this->privateinit ($url); $this->privatestartsession (' GET ');  }/** * The reason for getting an operation error * * @access public * @return void */function Printerror () {echo "error message:". $this->m_error; echo "
Specific return header:
"; foreach ($this->m_httphead as $k = + $v) {echo "$k + $v
\ r \ n "; }}/** * Determine if the answer to the header sent with the Get method is correct * * @access public * @return BOOL */function Isgetok () {if (Preg_match ("/^2/", $thi S->gethead ("Http-state")) {return TRUE; } else {$this->m_error. = $this->gethead ("Http-state"). "-". $this->gethead ("Http-describe"). "
"; return FALSE; }}/** * see if the page returned is text type * * @access public * @return BOOL */function Istext () {if (Preg_match ("/^2/", $this- GetHead ("Http-state")) && Preg_match ("/text|xml/i", $this->gethead ("Content-type")) {return TRUE; } else {$this->m_error. = "Content is non-text type or URL redirection
"; return FALSE; }}/** * Determines whether the returned Web page is of a specific type * * @access public * @param string $ctype Content Type * @return String */function Iscontenttype ($ctype) {if (Preg_match ("/^2/", $this->gethead ("Http-state") && $this->gethead ("Content-type") ==strtolower ( $ctype)) {return TRUE;} else {$this->m_error. = "wrong type". $this->gethead ("Content-type"). "
"; return FALSE; }}/** * download file with HTTP protocol * * @access public * @param string $savefilename Save file name * @return String */function Savetobi N ($savefilename) {if (! $this->isgetok ()) {return FALSE; if (@feof ($this->m_fp)) {$this->m_error = "The connection is closed! "; return FALSE; } $fp = fopen ($savefilename, "w"); while (!feof ($this->m_fp)) {fwrite ($fp, Fread ($this->m_fp, 1024)); } fclose ($this->m_fp); Fclose ($FP); return TRUE; /** * Save Web page content as text file * * @access public * @param string $savefilename Save file name * @return String */function Savetote XT ($savefilename) {if ($this->istext ()) {$this->savebinfile ($savefilename); } else {return ""; }}/** * Get the contents of a Web page with the HTTP protocol * * @access public * @return String */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 * * @access public * @param string $requestType Request type * @return String */function Privatestartsess Ion ($requestType = "GET") {if (! $this->privateopenhost ()) {$this->m_error. = "Error opening remote host!"; return FALSE; } $this->retry++; if ($this->gethead ("http-edition") = = "http/1.1") {$HTTPV = "http/1.1"; } else {$HTTPV = "http/1.0"; } $ps = Explode ('? ', $this->m_urlpath); $headString = "; Send a fixed start request header GET, host information if ($requestType = = "Get") {$headString. = "Get". $this->m_urlpath. "$HTTPV \ r \ n"; } else {$headString. = "POST". $ps [0]. "$HTTPV \ r \ n"; } $this->m_puthead["Host"] = $this->m_host; Send user-defined request header if (!isset ($this->m_puthead["accept")) {$this->m_puthead["accept"] = "*/*"; } if (!isset ($this->m_puthead["User-agent")) {$this->m_puthead["user-agent"] = "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2) "; } if (!issET ($this->m_puthead["Refer"]) {$this->m_puthead["Refer"] = "http://". $this->m_puthead["Host"]; } foreach ($this->m_puthead as $k = + $v) {$k = Trim ($k); $v = Trim ($v); if ($k! = "" && $v! = "") {$headString. = "$k: $v \ r \ n"; }} fputs ($this->m_fp, $headString); if ($requestType = = "POST") {$postdata = ""; if (count ($ps) >1) {for ($i =1; $i M_FP, "content-type:application/x-www-form-urlencoded\r\n"); Fputs ($this->m_fp, "content-length: $plen \ r \ n"); }//Send a fixed end request header The//HTTP1.1 protocol must specify closing the link after the document is finished, otherwise you cannot use feof to determine the end if ($HTTPV = = "http/1.1") {fputs ($this->m_fp, "Connecti") when reading the document On:close\r\n\r\n "); } else {fputs ($this->m_fp, "\ r \ n"); } if ($requestType = = "POST") {fputs ($this->m_fp, $postdata); }//Get Reply header status information $httpstas = Explode ("", Fgets ($this->m_fp,256)); $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 m_httphead["Http-describe". ". Trim ($httpstas [$i]); }//Get detailed answer header while (!feof ($this->m_fp)) {$line = Trim (fgets ($this->m_fp,256)); if ($line = = "") {break; } $hkey = ""; $hvalue = ""; $v = 0; for ($i =0; $i M_httphead[strtolower ($hkey)] = Trim ($hvalue); }}//If the connection is not properly closed, retry if (feof ($this->m_fp)) {if ($this->retry >) {return FALSE; } $this->privatestartsession ($requestType); }//Determine if the answer is 3xx (Preg_match ("/^3/", $this->m_httphead["Http-state")) {if ($this->jumpcount > 3) {R Eturn; } if (Isset ($this->m_httphead["Location")) {$newurl = $this->m_httphead["Location"]; if (Preg_match ("/^http/i", $newurl)) {$this->jumpopenurl ($newurl); } else {$newurl = $this->fillurl ($newurl); $this->jumpopenurl ($newurl); }} else {$this->m_error = "Unrecognized reply! "; }}}/** * Gets the value of an HTTP header * * @access public * @param string $headname header file name * @return String */function GetHead ($h Eadname) {$headname = Strtolower ($headname); return Isset ($this->m_httphead[$headname])? $this->m_httphead[$headname]: "; }/** * Sets the value of the HTTP header * * @access public * @param string $skey key * @param sTring $svalue Value * @return String */function Sethead ($skey, $svalue) {$this->m_puthead[$skey] = $svalue;}/** * Open Connection * * @access public * @return BOOL */function Privateopenhost () {if ($this->m_host== "") {return FALSE; } $errno = ""; $errstr = ""; $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 * * @access public * @return void */function close () {@fclose ($this->m_fp);}/** * Complete relative URL * * @access public * @param string $surl requires an incomplete address * @return String */function Fillurl ($surl) {$i = 0; $dstr = ""; $pstr = ""; $okurl = ""; $pathStep = 0; $surl = Trim ($surl); if ($surl = = "") {return ""; } $pos = Strpos ($surl, "#"); if ($pos >0) {$surl = substr ($surl, 0, $pos); if ($surl [0]== "/") {$okurl = "http://". $this->homeurl. $surl; } else if ($surl [0]==. ") {if (strlen ($surl) <=1) {return ""; } else if ($surl [1]== "/") {$okurl = "http://". $this->baseurlpath. " /". substr ($surl, 2,strlen ($surl)-2); } else {$urls = explode ("/", $surl); foreach ($urls as $u) {if ($u = = "...") {$pathStep + +; } else if ($i Baseurlpath); if (count ($urls) <= $pathStep) {return ""; } else {$pstr = "http://"; for ($i =0; $i <7) .$this-=""> baseurlpath. " /". $surl; } else if (Strtolower (substr ($surl, 0,7)) = = "http:/") {$okurl = $surl; } else {$okurl = "http://". $this->baseurlpath. " /". $surl; }} $okurl = Preg_replace ("/^ (http:\/\/)/I", "", $okurl); $okurl = Preg_replace ("/\/{1,}/", "/", $okurl); Return "http://" $OKURL; }}//end Class

I hope this article is helpful to everyone's PHP programming.

  • 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.