<?php
$str = file_get_contents ( "http://pic.hudong.com" );$url = "http://pic.hudong.com/";
//$content 就是要获取图像的字段
if (@ $content = file_get_contents ( $url )) {
//正则,用来匹配图像地址
$eregi_img_first = '|img([^>]*)src=([^>]*)>|s';
if (@  preg_match_all ( $eregi_img_first, $content, $out_img, PREG_SET_ORDER )) {
for($i = 0; $i < count ( $out_img ); $i ++) {
//--处理地址,除去多余字符
$in = 1;
$split_str = substr ( $out_img [$i] [2], 0, 1 );
if (('"' != $split_str) && ("'" != $split_str)) {
$split_str = " ";
$in = 0;
}
$img_pre = explode ( $split_str, trim ( $out_img [$i] [2] ) );
//--数组$img,存放得到的所有图像地址,$img[1]['img'] 即为第一张图像的地址
$img [$i] ['img'] = $img_pre [$in];
}
print_r ( $img );
$urls = $img [8] ["img"];
$str = file ( $urls );
echo "<br>" . $urls . "<br>";
$count = count ( $str );
for($j = 0; $j < $count; $j ++) {
$file .= $str [$j];

}
$listfile = "5xxx.jpg";
echo $listfile;
$fp = fopen ( $listfile, 'w' );
flock ( $fp, 2 );
fwrite ( $fp, $file );
fclose ( $fp );
for($i = 0; $i < count ( $img ); $i ++) {
$str="";
$str->flush();
$urls = $img [$i] ["img"];
$str = file ( $urls );
echo "<br>" . $urls . "<br>";
$count = count ( $str );
for($j = 0; $j < $count; $j ++) {
$file .= $str [$j];

}
$listfile = $i . "xxx.jpg";
echo $listfile;
$fp = fopen ( $listfile, 'w' );
flock ( $fp, 2 );
fwrite ( $fp, $file );
fclose ( $fp );

echo $file;
}

}
}?>上面是我的代码,可以下载图片,但是得到的每张图片都是一样的,也就是说$str = file ( $urls );
得到的值都是第一次得到的值,就是说本来很多图片,但是只得到了第一张图片,其他的都是重复的
该怎么解决啊?!

解决方案 »

  1.   

    Array
    (
        [0] => Array
            (
                [img] => images/logo.jpg
            )    [1] => Array
            (
                [img] => http://a2.att.hudong.com/22/94/01300000340773124894949669533_140.jpg
            )    [2] => Array
            (
                [img] => http://a3.att.hudong.com/61/98/01300000258678124228982955482_140.jpg
            )    [3] => Array
            (
                [img] => http://static.hudong.com/43/94/00100000000581124878945632753.jpg
            )    [4] => Array
            (
                [img] => http://static.hudong.com/33/90/00100000000581124878903359622.jpg
            )    [5] => Array
            (
                [img] => http://static.hudong.com/01/16/00100000001021124885164738641.jpg
            )    [6] => Array
            (
                [img] => http://static.hudong.com/81/13/00100000001021124885139584335.jpg
            )    [7] => Array
            (
                [img] => http://static.hudong.com/42/09/00100000001021124869094616552.jpg
            )    [8] => Array
            (
                [img] => http://static.hudong.com/03/97/00100000001021124867975914645.jpg
            )    [9] => Array
            (
                [img] => http://static.hudong.com/80/44/00100000000581124886442947964.jpg
            )    [10] => Array
            (
                [img] => http://a4.att.hudong.com/21/14/01300000280411123582147499176_s.jpg
            )    [11] => Array
            (
                [img] => http://static.hudong.com/35/86/00100000000581124693860511317.jpg
            )    [12] => Array
            (
                [img] => http://static.hudong.com/50/83/00100000000581124693830491926.jpg
            )    [13] => Array
            (
                [img] => http://static.hudong.com/56/78/00100000000581124693789523085.jpg
            )    [14] => Array
            (
                [img] => http://about.hudong.com/product/images/rili.gif
            )    [15] => Array
            (
                [img] => http://about.hudong.com/product/images/word_net.gif
            )    [16] => Array
            (
                [img] => http://about.hudong.com/product/images/hd_museum.gif
            )    [17] => Array
            (
                [img] => http://about.hudong.com/product/images/group.gif
            )    [18] => Array
            (
                [img] => http://static.hudong.com/30/99/00100000001341124894991589613.jpg
            )    [19] => Array
            (
                [img] => http://static.hudong.com/24/99/00100000001341124894999511782.jpg
            )    [20] => Array
            (
                [img] => http://static.hudong.com/26/43/00100000001521124894431556080.jpg
            )    [21] => Array
            (
                [img] => http://static.hudong.com/04/44/00100000001521124894446959258.jpg
            )    [22] => Array
            (
                [img] => http://static.hudong.com/86/48/00100000000661124894481678182.jpg
            )    [23] => Array
            (
                [img] => http://static.hudong.com/77/80/00100000000441124894807032966.jpg
            ))这个是程序执行结果,没有重复
      

  2.   

               $str->flush();
    这里报错,程序终止
    删掉就可以了
      

  3.   

    这个是没有重复,但是最后下载下来的图片全部都是一样的 ,那句flush的我已经删了好像是需要把其中的某个东西给清空,不然后面的循环取出来的图片都是开始的图片,但是不知道怎么做