note 26042 deleted from function.fopen by aidan
| From: | aidan@php.net | Date: | Tue, 28 Sep 2004 12:16:33 +0000 |
| Subject: | note 26042 deleted from function.fopen by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77506@lists.php.net to get a copy of this message | ||
Note Submitter: chirchik at r66 dot ru
----
If you're experiencing troubles with downloading files using the PROXY example posted here,
here's a workaround.
I've been trying to make the proxy example work for two hours when found out that it uses
fgets. That's why you can't get pictures properly downloaded.
I've made several changes, so here's a function to use.
function getthroughproxy
($myfiles,
$proxyhost="0.0.0.0",
$proxyport=0){
$errno="";
$errstr="";
$datei = fsockopen($proxyhost, $proxyport, &$errno, &$errstr,30);
if( !$datei )
{
fclose($resultfile);
return array('headers'=>false,
'content'=>false,
'errno'=>$errno,
'errstr'=>$errstr);
// ^^^ proxy not available
// You'll probably want to change this with return false;
// to use in an
// if($file=getthroughproxy){} manner.
// Well, it's up to You
} else {
fputs($datei,"GET $myfiles HTTP/1.0\n\n");
while (!feof($datei))
{
$zeile =$zeile.fread($datei,4096);
}
}
fclose($datei);
return array('headers'=>substr($zeile,0,strpos($zeile,"\r\n\r\n")),
'content'=>substr($zeile,strpos($zeile,"\r\n\r\n")+4),
'errno'=>$errno,
'errstr'=>$errstr);
}
Put Your proxy settings in the header of the function.
According to a comment in PHP web manual, you'll probably need to replace \n\n with \r\n\r\n or
vice versa for some proxies.
Usage:
foreach($imgurls as $num=>$url){
$img=getthroughproxy($url);
if($img['content']!==false){
if($new=fopen(
$newurl="/web/news/imageheap".strrchr($url,"/"),"w")){
fputs($new,$img['content']);
fclose($new);
// print_r($img);
foreach($img as $ki=>$vi) unset($img[$ki]);
unset($img);
echo "$url written as $newurl<br>\n";
}else{
echo "Error opening $newurl for writing<br>\n";
}
}else{
echo "Error opening $url<br>\n";
// print_r($img);
}
}