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
Userdata
Name
shiplu
Scriptlanguage
PHP
Tabwidth
6
Date
08/26/2008 12:22:21 am
IP
202.56.7.134
Description
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
Code (Hide rownumbers )
$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);