Foreach Loop Only Iterating Once - PHP -
i looking php script grab images few websites, couldn't find 1 universal , allowed scraping of multiple pages.. built one.
it asks save-to path , list of web-pages scrape images from, delimited comma. script explodes user inputted web pages comma , loops through each one. scrape first web page fine skips rest.
here entire script. feel free copy localhost , run it, you'll see mean.
<?php error_reporting( 0 ); if(isset($_post['doit'])){ $scrapethese = explode(",",$_post['website']); foreach($scrapethese $scrapethis){ $cleanit = str_replace(" ", "", $scrapethis); $html = file_get_contents($cleanit); $dom = new domdocument; $dom->loadhtml($html); $dom->preservewhitespace = false; $imgs = $dom->getelementsbytagname('img'); foreach($imgs $img){ $fullimgpath = $img->getattribute('src'); $slashexp = explode('/', $fullimgpath); $lastindex = count($slashexp)-1; $shortpath = $slashexp[$lastindex]; $filename = $_post['folder']."\\".$shortpath; if(copy($fullimgpath, $filename)){ echo $slashexp[$lastindex]." saved<br />"; }else{ echo "<b style='color:red;'>could not save img: </b>".$filename."<br />"; } } } }else{ ?> <form method="post" action="<?php echo $_server['php_self']; ?>"> <input type="hidden" name="doit" value="x" /> <label>the complete path page you'd scrape. may enter multiple paths seperated commas.</label><br /> <textarea name="website" /></textarea><br /> <label>path folder want save images to</label><br /> <input type="text" name="folder" /><br /> <input type="submit" value="save images" /> </form> <?php } ?> if can provide insight why script not looping through websites when multiple provided appreciate it.
Comments
Post a Comment