首頁 後端開發 php教程 支援斷點續傳的PHP 下載遠端檔案類

支援斷點續傳的PHP 下載遠端檔案類

Jul 25, 2016 am 08:44 AM

PHP 다운로드 원격 파일 클래스, 중단점 재개 다운로드 지원, 코드에는 특정 호출 지침이 포함되어 있습니다. 프로그램은 주로 HTTP 프로토콜을 사용하여 파일을 다운로드합니다. HTTP1.1 프로토콜은 문서가 끝난 후 링크를 ​​닫아야 함을 지정해야 합니다. 그렇지 않으면 문서를 읽을 때 feof를 사용하여 끝을 판단할 수 없습니다. 자세한 내용은 소스 코드를 다운로드하여 확인하세요.

  1. /**
  2. * 下載遠端檔案類別支援斷點續傳
  3. */
  4. class HttpDownload {
  5. private $m_url = "";
  6. private $m_path = "";
  7. private $m_path = " ";
  8. 私人$m_scheme = "http";
  9. 私人$m_host = "";
  10. 私人$m_port = "80";
  11. 私人$m_user = "";
  12. 私人$ m_pass = "";
  13. 私人$m_path = "/";
  14. 私人$m_query = "";
  15. 私人$m_fp = "";
  16. 私人$m_error = "";
  17. 私人 $ m_httphead = "" ;
  18. private $m_html = "";
  19. /**
  20. * 初始化
  21. */
  22. public function PrivateInit($url){
  23. $urls = "";
  24. $urls = @parse_url($url);
  25. $this->m_url = $url;
  26. if(is_array($urls)) {
  27. $this->m_host = $urls ["主機"];
  28. if(!empty($urls["scheme"])) $this->m_scheme = $urls["scheme"];
  29. if(!empty($urls[") user" ])) $this->m_user = $urls["用戶"];
  30. if(!empty($urls["pass"])) $this->m_pass = $urls["pass" ];
  31. if(!empty($urls["port"])) $this->m_port = $urls["port"];
  32. if(!empty($urls["path"]) ) $this-> m_path = $urls["path"];
  33. $this->m_urlpath = $this->m_path;
  34. if(!empty($urls["query"])) {
  35. $this-> m_query = $urls["query"];
  36. $this->m_urlpath .= "?".$this->m_query;
  37. }
  38. }
  39. }
  40. /* *
  41. * 開啟指定網址
  42. */
  43. function OpenUrl($url) {
  44. #重設各參數
  45. $this->m_url = "";
  46. $this->m_urlpath = "";
  47. $this->m_scheme = "http";
  48. $this->m_host = "";
  49. $this->m_port = "80 ";
  50. $this->m_user = "";
  51. $this->m_pass = "";
  52. $this->m_path = "/";
  53. $this->; m_query = "";
  54. $this->m_error = "";
  55. $this->m_httphead = "" ;
  56. $this->m_html = "";
  57. $this->Close( );
  58. # 初始化系統
  59. $this->PrivateInit($url);
  60. $this->PrivateStartSession();
  61. }
  62. /* *
  63. * 取得某操作錯誤的原因
  64. */
  65. public function printError() {
  66. echo "錯誤訊息:".$this->m_error;
  67. echo "具體回傳頭:
    ";
  68. foreach($this->m_httphead as $k=>$v) {
  69. echo "$k =>; $v
    rn";
  70. }
  71. }
  72. /**
  73. * 判別用Get方法發送的頭的應答結果是否正確
  74. */
  75. public function IsGetOK() {
  76. if( ereg("^2 ",$this->GetHead(" http-state")) ) {
  77. 回傳true;
  78. } else {
  79. $this->m_error .= $this->GetHead("http -狀態」)。 ";
  80. return false;
  81. }
  82. }
  83. /**
  84. * 看看回傳的網頁是否為text類型
  85. */
  86. public function IsText() {
  87. if (ereg("^2" ) ,$this->GetHead("http-state")) && eregi("^text",$this->; GetHead("content-type"))) {
  88. return true
  89. } else {
  90. $this->m_error .= "內容為非文字型別
    ";
  91. return; false;
  92. }
  93. }
  94. /**
  95. * 判斷傳回的網頁是否為特定的類型
  96. */
  97. public function IsContentType($ctype) {
  98. if (ereg("^2",$this->GetHead) ("http-state")) && $this->GetHead("content-type") == strtolower ( $ctype)) {
  99. return true
  100. } else {
  101. $this-> ;m_error .= "型別錯誤".$this->GetHead("content-type")."
    " ;
  102. return false;
  103. }
  104. }
  105. /**
  106. * 用 HTTP 協定下載檔案
  107. */
  108. public function SaveToBin($savefilename) {
  109. if (!$this- > IsGetOK()) return false;
  110. if (@feof($this->IsGetOK()) return false;
  111. if (@feof($this->IsGetOK()) m_fp)) {
  112. $ this->m_error = "連線已經關閉!";
  113. 回傳 false; } $fp = fopen($savefilename,"w") 或 die("寫入檔案 $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. * 웹페이지 내용을 텍스트 파일로 저장
  122. */
  123. 공용 함수 SaveToText($savefilename) {
  124. if ( $this->IsText()) {
  125. $this->SaveBinFile($savefilename);
  126. } else {
  127. return "";
  128. }
  129. }
  130. /**
  131. * HTTP 프로토콜을 사용하여 웹페이지 콘텐츠 가져오기
  132. */
  133. 공용 함수 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. * HTTP 세션 시작
  145. */
  146. public function PrivateStartSession() {
  147. if (!$this->PrivateOpenHost() ) {
  148. $this->m_error .= "打开远程主机出错!";
  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." $httpvrn");
  157. fputs($this->m_fp,"Host: ".$this->m_host."rn");
  158. fputs($this->m_fp,"Accept: */*rn");
  159. fputs($this->m_fp,"User-Agent: Mozilla/4.0(호환 가능; MSIE 6.0; Windows NT 5.2)rn");
  160. #HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无법使用feof判断结束
  161. if ($httpv=="HTTP/1 .1") {
  162. fputs($this->m_fp,"연결: Closernrn");
  163. } else {
  164. fputs($this->m_fp,"rn");
  165. }
  166. $ httpstas = fgets($this->m_fp,256);
  167. $httpstas = 분할(" ",$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. * HTTP 헤더 값 가져오기
  184. */
  185. 공개 함수 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. * 연결 열기
  195. */
  196. 공용 함수 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. * 연결 끊기
  208. */
  209. 공용 함수 Close(){
  210. @ fclose($this->m_fp);
  211. }
  212. }
  213. #两种使사용방법,分别如下:
  214. #打开网页
  215. $httpdown = new HttpDownload ();
  216. $httpdown->OpenUrl("http://www.google.com.hk");
  217. echo $httpdown->GetHtml();
  218. $httpdown->닫기 ();
  219. #下载文件
  220. $file = new HttpDownload(); # 实例化类
  221. $file->OpenUrl("http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # 远程文件地址
  222. $file->SaveToBin("rust020.pdf"); # 保存路径及文件name
  223. $file->Close(); # 释放资源
  224. ?>
复代码

PHP


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1657
14
CakePHP 教程
1415
52
Laravel 教程
1309
25
PHP教程
1257
29
C# 教程
1230
24
在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

會話如何劫持工作,如何在PHP中減輕它? 會話如何劫持工作,如何在PHP中減輕它? Apr 06, 2025 am 12:02 AM

會話劫持可以通過以下步驟實現:1.獲取會話ID,2.使用會話ID,3.保持會話活躍。在PHP中防範會話劫持的方法包括:1.使用session_regenerate_id()函數重新生成會話ID,2.通過數據庫存儲會話數據,3.確保所有會話數據通過HTTPS傳輸。

您如何在PHP中有效處理異常(嘗試,捕捉,最後,投擲)? 您如何在PHP中有效處理異常(嘗試,捕捉,最後,投擲)? Apr 05, 2025 am 12:03 AM

在PHP中,異常處理通過try,catch,finally,和throw關鍵字實現。 1)try塊包圍可能拋出異常的代碼;2)catch塊處理異常;3)finally塊確保代碼始終執行;4)throw用於手動拋出異常。這些機制幫助提升代碼的健壯性和可維護性。

說明PHP中的不同錯誤類型(注意,警告,致命錯誤,解析錯誤)。 說明PHP中的不同錯誤類型(注意,警告,致命錯誤,解析錯誤)。 Apr 08, 2025 am 12:03 AM

PHP中有四種主要錯誤類型:1.Notice:最輕微,不會中斷程序,如訪問未定義變量;2.Warning:比Notice嚴重,不會終止程序,如包含不存在文件;3.FatalError:最嚴重,會終止程序,如調用不存在函數;4.ParseError:語法錯誤,會阻止程序執行,如忘記添加結束標籤。

包括,require,incement_once,require_once之間有什麼區別? 包括,require,incement_once,require_once之間有什麼區別? Apr 05, 2025 am 12:07 AM

在PHP中,include,require,include_once,require_once的區別在於:1)include產生警告並繼續執行,2)require產生致命錯誤並停止執行,3)include_once和require_once防止重複包含。這些函數的選擇取決於文件的重要性和是否需要防止重複包含,合理使用可以提高代碼的可讀性和可維護性。

PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

什麼是HTTP請求方法(獲取,發布,放置,刪除等),何時應該使用? 什麼是HTTP請求方法(獲取,發布,放置,刪除等),何時應該使用? Apr 09, 2025 am 12:09 AM

HTTP請求方法包括GET、POST、PUT和DELETE,分別用於獲取、提交、更新和刪除資源。 1.GET方法用於獲取資源,適用於讀取操作。 2.POST方法用於提交數據,常用於創建新資源。 3.PUT方法用於更新資源,適用於完整更新。 4.DELETE方法用於刪除資源,適用於刪除操作。

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

See all articles