php 页面抓取和图片自动下载代码!
<?php
$file = fopen (“http://www.looooking.cn/“, “r”);
if (!$file) {
echo “<p>不能打开远程文件!n”;
exit;
}
while (!feof ($file)) {
$line = fgets ($file, 1024);
if (eregi (“src=”(.*)/(.*)/(.*).(jpg|JPG|gif|GIF|png)”, $line, $out )) {
$title = $out[0];
eregi (“(jpg|JPG|gif|GIF|png)”, $title, $after);
//echo “$after[0] n”;
$name = $out[3];
//echo “$name n”;
$img = file_get_contents(‘http://www.looooking.cn/’.$out[2].’/’.$name.’.’.$after[0]);
$path = $out[2];
echo(!file_exists($path));
if (!file_exists($path))
{
createFolder($out[2]);
}
file_put_contents($out[2].’/’.$name.’.’.$after[0],$img);
}
echo “完成下载 n”;
}
function createFolder($path)
{
if (!file_exists($path))
{
createFolder(dirname($path));
mkdir($path, 0777);
}
}
?>