php批量下载文件 代码,PHP批量下载指定路径图片

代碼寫得太糟糕了,還有幾處錯誤,幫你重寫一份。

在一個循環里多次調用 addPage 就行了。

如果已經有 url 的話,也可以直接調用 download。

for ($i = 0; $i < 99; ++$i)

download('http://cdn.image.com/static/' . $i . '.png', DOWNLOAD_PATH . $i . '.png');

效率有點低,可以考慮改用 culr_multi_* 系列函數。

a97b5d4a2127caa6151acec3e3082300.png

namespace Spider;

define('DOWNLOAD_PATH', './imgDownload/');

spider();

function spider($path = DOWNLOAD_PATH) {

$urls = [];

addPage('http://www.baidu.com', $urls);

addPage('http://www.taobao.com', $urls);

var_dump($urls);

// error_reporting(0);

foreach ($urls as $url => $referer) {

// echo $url, "\n";

$filename = pathinfo($url, PATHINFO_FILENAME) . '.' . pathinfo($url, PATHINFO_EXTENSION);

download($url, $path . $filename, $referer);

}

// error_reporting(1);

}

function addPage($page, &$urls) {

$cur = extractPage($page);

for ($i = 0, $n = count($cur); $i < $n; ++$i) {

$j = $cur[$i];

if (!isset($urls[$j]))

$urls[$j] = $page;

}

}

function extractPage($page, $reg = '//') {

$content = file_get_contents($page);

// $content = '1.png';

preg_match_all($reg, $content, $matches);

return $matches[1];

}

function download($url, $file, $referer = '') {

$url = abs_url($url, $referer);

echo $url, "\n";

$opts = [

'http' => [

'method' => 'GET',

'header' => "Accept-language: en\r\n" .

// "Cookie: \r\n" .

"Referer: " . $url . "\r\n"

]

];

$context = stream_context_create($opts);

file_put_contents($file, file_get_contents($url, false, $context));

return $file;

}

function abs_url($url, $referer) {

$com = parse_url($url);

if (!isset($com['scheme']))

$com['scheme'] = 'http';

if ($referer !== '') {

$ref = parse_url($referer);

if (!isset($com['host'])) {

if (isset($ref['host'])) {

$com['host'] = $ref['host'];

if (!isset($ref['path']))

$ref['path'] = '/';

}

if (isset($com['path'][0])) {

if ($com['path'][0] !== '/')

$com['path'] = $ref['path'] . $com['path'];

} else if (isset($ref['host'])) {

$com['path'] = $ref['path'];

}

} else {

if (!isset($com['path']))

$com['path'] = '';

}

}

return unparse_url($com);

}

function unparse_url($com) {

return (isset($com['host']) ? ($com['scheme'] . '://' . $com['host']) : '') . $com['path'] . (isset($com['query']) ? '?'.$com['query'] : '');

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值