$this->lfp = fopen($this->localfilename,"wb");
if($this->lfp===false){return false;}
if(function_exists('fsockopen')){
$pu = parse_url($this->download_url);
$request="POST {$pu['path']} HTTP/1.1\r\n";
$request.="Host: {$pu['host']}\r\n";
$request.="Content-type: application/x-www-form-urlencoded\r\n";
$request.=implode("\r\n",$headers)."\r\n";
$request.="Connection: close\r\n";
$request.="Content-length: ".strlen($postdata)."\r\n\r\n";
$sock = fsockopen($pu['host'],80,$errno,$errstr);
fwrite($sock,$request);
$this->debugMessage("Headers Sent\r\n".$request);
fwrite($sock,$postdata);
$headers="";
do{
$headers.=fread($sock,128);
}while(strpos($headers,"\r\n\r\n")===false);
$pos = strpos($headers,"\r\n\r\n");
$headers = split("\r\n\r\n",$headers);
$headers = $headers[0];
$this->debugMessage("Headers recieved".$headers."\r\n");
$this->debugMessage("Downloading started. . .\r\n");
$content = substr($headers,$pos+4);
if($content!==false){
$this->doCallback(fwrite($this->lfp,$content));
}
while(!feof($sock)){
$this->doCallback(fwrite($this->lfp,fread($sock,$this->read_chunk_size)));
}
$this->debugMessage("Download finished\r\n");
fclose($sock);
}else {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$this->download_url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if(!empty($this->cookiefile)){
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiefile);
}
curl_setopt($ch,CURLOPT_FILE,$this->lfp);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch);
curl_close($ch);
}
fclose($this->lfp);