php中下载网站文件

作者:源码世界时间:2014-04-25分类:PHP评论:0浏览:4964


1、fsockopen方法

<?php
$url = “http://www.ymsky.net/applications/themes/four/Public/common/images/logo.png”;
$t = parse_url($url);
$host = $t['host'];
$file = $t['path'];
$fp = fsockopen($host,80, $errno, $errstr, 30);
if($fp)
{
$header = “GET $file HTTP/1.1rn”;
$header .= “Host: $hostrn”;
$header .= “User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1rn”;
$header .= “Referer: http://$hostrn”;
$header .= “Connection: Closernrn”;
fwrite($fp, $header);
$jpg = fopen($path . basename($file), “wb”);
while (!feof($fp))
{
$s = fgets($fp,128);
fwrite($jpg,$s);
}
fclose($jpg);
fclose($fp);
}
?>
2、curl方法。需要php中启用curl。来自:http://www.askapache.com/php/curl-multi-downloads.html,我加上了几个选项:REFERER检查等。 

<?php set_time_limit(0); ini_set(‘display_errors’,true);//Just in case we get some errors, let us know….

$host = “www.example.net”; $urls=array( “http://www.ymsky.net/applications/themes/four/Public/common/images/logo.png”, “http://www.ymsky.net/applications/themes/four/Public/common/images/logo.png” );

$save_to=’./’;

$mh = curl_multi_init();

foreach ($urls as $i => $url) { $g=$save_to.basename($url); if(!is_file($g)) { $conn[$i]=curl_init($url);

$fp[$i]=fopen ($g, “wb”);

curl_setopt($conn[$i], CURLOPT_USERAGENT, “Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1″); curl_setopt($conn[$i], CURLOPT_REFERER, “http://$host”); curl_setopt($conn[$i], CURLOPT_FILE, $fp[$i]); curl_setopt($conn[$i], CURLOPT_HEADER ,0); curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT,600); curl_setopt($conn[$i], CURLOPT_FOLLOWLOCATION, 1); //curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 0); //curl_setopt($conn[$i], CURLOPT_VERBOSE, 0);

curl_multi_add_handle ($mh,$conn[$i]); } }

do { $n=curl_multi_exec($mh,$active); } while ($active);

foreach ($urls as $i => $url) { curl_multi_remove_handle($mh,$conn[$i]); curl_close($conn[$i]); fclose ($fp[$i]); }

curl_multi_close($mh); ?>

如果您没有找到适合你的解决方案,请联系我们寻求帮助支付鼓励

标签:foreach  
返回顶部
分享按钮