gnopaste

Sorry for this ugly message. We are searching for some translators.
Please send me an eMail or jabber: t4c@im.digital-bit.ch
Got: de, en, pl, ro, lu, ru, es - Need: everything else

Name
shiplu
Scriptlanguage
PHP
Tabwidth
6
Date
08/26/2008 12:22:21 am
IP
202.56.7.134

Downloading File By PHP
By Shiplu@cmyweb.net

the code is a downloading function of a class.
the class has some methods need to be explained.
1) debugMessage, it sends message to a call back function to a caller
2) doCallback, sends the amount of bytes read to a call back function to the caller\
3) localfilename is the file where the remote file will be downloaded
4) lfp is localfilepointer

  1.               $this->lfp = fopen($this->localfilename,"wb");
  2.             
  3.             if($this->lfp===false){return false;}
  4.  
  5.             if(function_exists('fsockopen')){
  6.                   $pu = parse_url($this->download_url);
  7.                   $request="POST {$pu['path']} HTTP/1.1\r\n";
  8.                   $request.="Host: {$pu['host']}\r\n";
  9.                   $request.="Content-type: application/x-www-form-urlencoded\r\n";
  10.                   $request.=implode("\r\n",$headers)."\r\n";
  11.                   $request.="Connection: close\r\n";
  12.                   $request.="Content-length: ".strlen($postdata)."\r\n\r\n";
  13.                   
  14.                   $sock = fsockopen($pu['host'],80,$errno,$errstr);
  15.                   
  16.                   fwrite($sock,$request);
  17.                   
  18.                   $this->debugMessage("Headers Sent\r\n".$request);
  19.                   
  20.                   fwrite($sock,$postdata);
  21.                   
  22.                   $headers="";
  23.                   do{
  24.                         $headers.=fread($sock,128);
  25.                   }while(strpos($headers,"\r\n\r\n")===false);
  26.                   
  27.                   $pos = strpos($headers,"\r\n\r\n");
  28.                   $headers = split("\r\n\r\n",$headers);
  29.                   $headers = $headers[0];
  30.                   
  31.                   $this->debugMessage("Headers recieved".$headers."\r\n");
  32.                   $this->debugMessage("Downloading started. . .\r\n");
  33.                   $content = substr($headers,$pos+4);
  34.                   if($content!==false){
  35.                         $this->doCallback(fwrite($this->lfp,$content));
  36.                   }
  37.                   while(!feof($sock)){
  38.                         $this->doCallback(fwrite($this->lfp,fread($sock,$this->read_chunk_size)));
  39.                   }
  40.                   $this->debugMessage("Download finished\r\n");
  41.                   
  42.                   fclose($sock);
  43.                   
  44.             }else {
  45.                   $ch = curl_init();
  46.                   curl_setopt($ch, CURLOPT_URL,$this->download_url);
  47.                   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  48.                   if(!empty($this->cookiefile)){
  49.                         curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiefile);
  50.                         curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiefile);
  51.                   }
  52.                   curl_setopt($ch,CURLOPT_FILE,$this->lfp);
  53.                   curl_setopt($ch, CURLOPT_POST,1);
  54.                   curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
  55.  
  56.                   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  57.  
  58.                   curl_exec($ch);
  59.                   curl_close($ch);
  60.             }
  61.             fclose($this->lfp);
submitter » gnopaster | imprint « imprint     
» Terms of use «
» digital bit dot ch - t4c's new home «