国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php遠程下載類分享

2024-05-04 23:45:01
字體:
來源:轉載
供稿:網友

這篇文章主要為大家分享了php遠程下載類,用戶可以將下載文件到主機,感興趣的小伙伴們可以參考一下

本文實例為大家分享了php遠程下載類,如下
 

  1. <?php  
  2. /**  
  3. * 下載遠程文件類支持斷點續傳  
  4. */ 
  5. class HttpDownload {  
  6. private $m_url = "";  
  7. private $m_urlpath = "";  
  8. private $m_scheme = "http";  
  9. private $m_host = "";  
  10. private $m_port = "80";  
  11. private $m_user = "";  
  12. private $m_pass = "";  
  13. private $m_path = "/";  
  14. private $m_query = "";  
  15. private $m_fp = "";  
  16. private $m_error = "";  
  17. private $m_httphead = "" ;  
  18. private $m_html = "";  
  19.   
  20. /**  
  21. * 初始化  
  22. */ 
  23. public function PrivateInit($url){  
  24. $urls = "";  
  25. $urls = @parse_url($url);  
  26. $this->m_url = $url;  
  27. if(is_array($urls)) {  
  28. $this->m_host = $urls["host"];  
  29. if(!emptyempty($urls["scheme"])) $this->m_scheme = $urls["scheme"];  
  30. if(!emptyempty($urls["user"])) $this->m_user = $urls["user"];  
  31. if(!emptyempty($urls["pass"])) $this->m_pass = $urls["pass"];  
  32. if(!emptyempty($urls["port"])) $this->m_port = $urls["port"];  
  33. if(!emptyempty($urls["path"])) $this->m_path = $urls["path"];  
  34. $this->m_urlpath = $this->m_path;  
  35. if(!emptyempty($urls["query"])) {  
  36. $this->m_query = $urls["query"];  
  37. $this->m_urlpath .= "?".$this->m_query;  
  38. }  
  39. }  
  40. }  
  41.   
  42. /**  
  43. * 打開指定網址  
  44. */ 
  45. function OpenUrl($url) {  
  46. #重設各參數  
  47. $this->m_url = "";  
  48. $this->m_urlpath = "";  
  49. $this->m_scheme = "http";  
  50. $this->m_host = "";  
  51. $this->m_port = "80";  
  52. $this->m_user = "";  
  53. $this->m_pass = "";  
  54. $this->m_path = "/";  
  55. $this->m_query = "";  
  56. $this->m_error = "";  
  57. $this->m_httphead = "" ;  
  58. $this->m_html = "";  
  59. $this->Close();  
  60. #初始化系統  
  61. $this->PrivateInit($url);  
  62. $this->PrivateStartSession();  
  63. }  
  64.   
  65. /**  
  66. * 獲得某操作錯誤的原因  
  67. */ 
  68. public function printError() {  
  69. echo "錯誤信息:".$this->m_error;  
  70. echo "具體返回頭:<br>";  
  71. foreach($this->m_httphead as $k=>$v) {  
  72. echo "$k => $v <br>/r/n";  
  73. }  
  74. }  
  75.   
  76. /**  
  77. * 判別用Get方法發送的頭的應答結果是否正確  
  78. */ 
  79. public function IsGetOK() {  
  80. ifereg("^2",$this->GetHead("http-state")) ) {  
  81. return true;  
  82. else {  
  83. $this->m_error .= $this->GetHead("http-state")." - ".$this->GetHead("http-describe")."<br>";  
  84. return false;  
  85. }  
  86. }  
  87.   
  88. /**  
  89. * 看看返回的網頁是否是text類型  
  90. */ 
  91. public function IsText() {  
  92. if (ereg("^2",$this->GetHead("http-state")) && eregi("^text",$this->GetHead("content-type"))) {  
  93. return true;  
  94. else {  
  95. $this->m_error .= "內容為非文本類型<br>";  
  96. return false;  
  97. }  
  98. }  
  99. /**  
  100. * 判斷返回的網頁是否是特定的類型  
  101. */ 
  102. public function IsContentType($ctype) {  
  103. if (ereg("^2",$this->GetHead("http-state")) && $this->GetHead("content-type") == strtolower($ctype)) {  
  104. return true;  
  105. else {  
  106. $this->m_error .= "類型不對 ".$this->GetHead("content-type")."<br>";  
  107. return false;  
  108. }  
  109. }  
  110.   
  111. /**  
  112. * 用 HTTP 協議下載文件  
  113. */ 
  114. public function SaveToBin($savefilename) {  
  115. if (!$this->IsGetOK()) return false;  
  116. if (@feof($this->m_fp)) {  
  117. $this->m_error = "連接已經關閉!";  
  118. return false;  
  119. }  
  120. $fp = fopen($savefilename,"w"or die("寫入文件 $savefilename 失?。?quot;);  
  121. while (!feof($this->m_fp)) {  
  122. @fwrite($fp,fgets($this->m_fp,256));  
  123. }  
  124. @fclose($this->m_fp);  
  125. return true;  
  126. }  
  127.   
  128. /**  
  129. * 保存網頁內容為 Text 文件  
  130. */ 
  131. public function SaveToText($savefilename) {  
  132. if ($this->IsText()) {  
  133. $this->SaveBinFile($savefilename);  
  134. else {  
  135. return "";  
  136. }  
  137. }  
  138.   
  139. /**  
  140. * 用 HTTP 協議獲得一個網頁的內容  
  141. */ 
  142. public function GetHtml() {  
  143. if (!$this->IsText()) return "";  
  144. if ($this->m_html!=""return $this->m_html;  
  145. if (!$this->m_fp||@feof($this->m_fp)) return "";  
  146. while(!feof($this->m_fp)) {  
  147. $this->m_html .= fgets($this->m_fp,256);  
  148. }  
  149. @fclose($this->m_fp);  
  150. return $this->m_html;  
  151. }  
  152.   
  153. /**  
  154. * 開始 HTTP 會話  
  155. */ 
  156. public function PrivateStartSession() {  
  157. if (!$this->PrivateOpenHost()) {  
  158. $this->m_error .= "打開遠程主機出錯!";  
  159. return false;  
  160. }  
  161. if ($this->GetHead("http-edition")=="HTTP/1.1") {  
  162. $httpv = "HTTP/1.1";  
  163. else {  
  164. $httpv = "HTTP/1.0";  
  165. }  
  166. fputs($this->m_fp,"GET ".$this->m_urlpath." $httpv/r/n");  
  167. fputs($this->m_fp,"Host: ".$this->m_host."/r/n");  
  168. fputs($this->m_fp,"Accept: */*/r/n");  
  169. fputs($this->m_fp,"User-Agent: Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2)/r/n");  
  170. #HTTP1.1協議必須指定文檔結束后關閉鏈接,否則讀取文檔時無法使用feof判斷結束  
  171. if ($httpv=="HTTP/1.1") {  
  172. fputs($this->m_fp,"Connection: Close/r/n/r/n");  
  173. else {  
  174. fputs($this->m_fp,"/r/n");  
  175. }  
  176. $httpstas = fgets($this->m_fp,256);  
  177. $httpstas = split(" ",$httpstas);  
  178. $this->m_httphead["http-edition"] = trim($httpstas[0]);  
  179. $this->m_httphead["http-state"] = trim($httpstas[1]);  
  180. $this->m_httphead["http-describe"] = "";  
  181. for ($i=2;$i<count($httpstas);$i++) {  
  182. $this->m_httphead["http-describe"] .= " ".trim($httpstas[$i]);  
  183. }  
  184. while (!feof($this->m_fp)) {  
  185. $line = str_replace("/"","",trim(fgets($this->m_fp,256)));  
  186. if($line == ""break;  
  187. if (ereg(":",$line)) {  
  188. $lines = split(":",$line);  
  189. $this->m_httphead[strtolower(trim($lines[0]))] = trim($lines[1]);  
  190. }  
  191. }  
  192. }  
  193.   
  194. /**  
  195. * 獲得一個Http頭的值  
  196. */  
  197. public function GetHead($headname) {  
  198. $headname = strtolower($headname);  
  199. if (isset($this->m_httphead[$headname])) {  
  200. return $this->m_httphead[$headname];  
  201. else {  
  202. return "";  
  203. }  
  204. }  
  205.   
  206. /**  
  207. * 打開連接  
  208. */ 
  209. public function PrivateOpenHost() {  
  210. if ($this->m_host==""return false;  
  211. $this->m_fp = @fsockopen($this->m_host, $this->m_port, &$errno, &$errstr,10);  
  212. if (!$this->m_fp){  
  213. $this->m_error = $errstr;  
  214. return false;  
  215. else {  
  216. return true;  
  217. }  
  218. }  
  219.   
  220. /**  
  221. * 關閉連接  
  222. */ 
  223. public function Close(){  
  224. @fclose($this->m_fp);  
  225. }  
  226. }  
  227.   
  228. #兩種使用方法,分別如下:  
  229.   
  230. #打開網頁  
  231. $httpdown = new HttpDownload();  
  232. $httpdown->OpenUrl("http://www.google.com.hk");  
  233. echo $httpdown->GetHtml();  
  234. $httpdown->Close();  
  235.   
  236.   
  237. #下載文件  
  238. $file = new HttpDownload(); # 實例化類  
  239. $file->OpenUrl("http://dldir1.qq.com/qqfile/qq/QQ8.2/17724/QQ8.2.exe"); # 遠程文件地址  
  240. $file->SaveToBin("qq.exe"); # 保存路徑及文件名  
  241. $file->Close(); # 釋放資源 

以上就是本文的全部內容,希望對大家的學習有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 北碚区| 湘潭市| 榆树市| 根河市| 绥化市| 淮滨县| 潼关县| 当雄县| 永年县| 江山市| 泰和县| 社会| 北流市| 凤山县| 滕州市| 赤城县| 全州县| 远安县| 顺义区| 阳泉市| 承德县| 巴林右旗| 伽师县| 平潭县| 海阳市| 措勤县| 九寨沟县| 宁远县| 鹤庆县| 古浪县| 隆安县| 敦化市| 张掖市| 金溪县| 寻甸| 微博| 娱乐| 平顶山市| 甘德县| 苍溪县| 秦安县|