PHP remote file download class, supports resumable download, the code contains specific call instructions. The program uses the HTTP protocol to download files. the HTTP1.1 protocol must specify that the link is closed after the document ends. otherwise, feof cannot be used to judge the end of the document. you can use either of the following methods, for details, download and view the source code.
- /**
- * Supports resumable Upload for remote file downloads.
- */
- 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 = "";
- /**
- * Initialization
- */
- 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;
- }
- }
- }
- /**
- * Open the specified URL
- */
- Function OpenUrl ($ url ){
- # Resetting parameters
- $ 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 ();
- # Initialize the system
- $ This-> PrivateInit ($ url );
- $ This-> PrivateStartSession ();
- }
- /**
- * Obtain the cause of an operation error.
- */
- Public function printError (){
- Echo "error message:". $ this-> m_error;
- Echo "specific return headers:
";
- Foreach ($ this-> m_httphead as $ k => $ v ){
- Echo "$ k => $ v
\ R \ n ";
- }
- }
- /**
- * Determine whether the response results of the headers sent using the Get method are 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 ")."
";
- Return false;
- }
- }
- /**
- * Check whether the returned webpage is of the text type.
- */
- Public function IsText (){
- If (ereg ("^ 2", $ this-> GetHead ("http-state") & eregi ("^ text ", $ this-> GetHead ("content-type "))){
- Return true;
- } Else {
- $ This-> m_error. = "The content is non-text
";
- Return false;
- }
- }
- /**
- * Determine whether the returned webpage 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. = "The type is incorrect". $ this-> GetHead ("content-type ")."
";
- Return false;
- }
- }
- /**
- * Download an object over HTTP
- */
- Public function SaveToBin ($ savefilename ){
- If (! $ This-> IsGetOK () return false;
- If (@ feof ($ this-> m_fp )){
- $ This-> m_error = "The connection has been closed! ";
- Return false;
- }
- $ Fp = fopen ($ savefilename, "w") or die ("failed to write the file $ savefilename! ");
- While (! Feof ($ this-> m_fp )){
- @ Fwrite ($ fp, fgets ($ this-> m_fp, 256 ));
- }
- @ Fclose ($ this-> m_fp );
- Return true;
- }
- /**
- * Save the webpage content as a Text file
- */
- Public function SaveToText ($ savefilename ){
- If ($ this-> IsText ()){
- $ This-> SaveBinFile ($ savefilename );
- } Else {
- Return "";
- }
- }
- /**
- * Use HTTP to obtain the content of a webpage
- */
- 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 an HTTP session
- */
- Public function PrivateStartSession (){
- If (! $ This-> PrivateOpenHost ()){
- $ This-> m_error. = "an error occurred while opening the 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 ");
- # In HTTP1.1, the link must be closed after the document ends. otherwise, feof cannot be used to judge the end of the document.
- If ($ httpv = "HTTP/1.1 "){
- Fputs ($ this-> m_fp, "Connection: Close \ 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 $ 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]);
- }
- }
- }
- /**
- * Get 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 the 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 the connection.
- */
- Public function Close (){
- @ Fclose ($ this-> m_fp );
- }
- }
- # Use the following methods:
- # Open a webpage
- $ Httpdown = new HttpDownload ();
- $ Httpdown-> OpenUrl ("http://www.google.com.hk ");
- Echo $ httpdown-> GetHtml ();
- $ Httpdown-> Close ();
- # Download an object
- $ File = new HttpDownload (); # instantiate a class
- $ File-> OpenUrl ("http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # remote file address
- $ File-> SaveToBin ("rust020133"); # save path and file name
- $ File-> Close (); # release resources
- ?>
|