您的位置:首页 > 编程语言 > PHP开发

PHP配合JS实现某论坛图片批量下载

2014-11-06 15:38 423 查看
  学校论坛有个爆照帖,主要是为了下载某个id上传的照片。

  基本思路:写个用户脚本,对跟帖的n页做ajax轮询,获得的html当做一个节点插入dom树,然后查找页面获取所需id上传图片的url,post给服务器进行下载。

用户脚本部分

<?php
function downloadFile($url, $savePath, $name = '')
{
if ($name == '')
{
$header = get_headers($url, 1);
// 如果链接进行过跳转则匹配 location 之后的链接中的文件名
if (isset($header['Location'])) {
$name = basename(str_replace(strstr($header['Content-Type'][1], '?'), '', $header['Location']));
// 如果没有找到文件名则自动以当前时间为名称
$name = ($name == '') ? time() . str_replace('/', '', strstr($header['Content-Type'][1], '/')) : $name;
} else {
$name = basename(str_replace(strstr($header['Content-Type'], '?'), '', $url));
}
}
return file_put_contents($savePath .'/'. $name, file_get_contents($url));
}

//保存图片路径
$savePath = dirname(__FILE__)."\images";
$url = $_POST['content'];
downloadFile($url, $savePath);
// echo $savePath;
?>


download.php
  不知道为何download.php并不能下载所有图片,比如这张,但是因为不影响我的使用,也不深究了。

  如果按照如上的代码,download.php需要放在www下的download文件夹内,同文件夹内的还需要一个名为images的文件夹放图片。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: