2017-04-24 22:39 阅读 15


i am trying to make a page that allows me to grab and save images from another link , so here's what i want to add on my page:

  1. text box (to enter url that i want to get images from).
  2. save dialog box to specify the path to save images.

but what i am trying to do here i want to save images only from that url and from inside specific element.

for example on my code i say go to and from inside of element class="images" grab all images.

notes: not all images from the page, just from inside the element whether element has 3 images in it or 50 or 100 i don't care.

here's what i tried and worked using php

$html = file_get_contents('');
preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i',$html, $matches ); 
echo $matches[ 1 ][ 0 ];

this gets image name and path but what i am trying to make is a save dialog box and the code must save image directly into that path instead of echo it out

hope you understand

Edit 2

it's ok of Not having save dialog box. i must specify save path from the code

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongzaliang4492 dongzaliang4492 2017-04-24 22:58

    If you want something generic, you can use:

        $the_site = "";
        $the_tag = "div"; #
        $the_class = "images";
        $html = file_get_contents($the_site);
        $dom = new DOMDocument();
        $xpath = new DOMXPath($dom);
        foreach ($xpath->query('//'.$the_tag.'[contains(@class,"'.$the_class.'")]/img') as $item) {
            $img_src =  $item->getAttribute('src');
            print $img_src."


    Change the site, tag, which can be a div, span, a, etc. also change the class name.

    For example, change the values to:

    $the_site = "";
    $the_tag = "div"; #
    $the_class = "gravatar-wrapper-32";

    点赞 评论 复制链接分享
  • douza6300 douza6300 2017-04-24 23:33

    Maybe you should try HTML DOM Parser for PHP. I've found this tool recently and to be honest it works pretty well. It was JQuery-like selectors as you can see on the site. I suggest you to take a look and try something like:

    foreach ($html->find("<tag>") as $<tag>) //Start from the root (<html></html>) find the the parent tag you want to search in instead of <tag> (e.g "div" if you want to search in all divs)
        foreach ($<tag>->find("img") as $img) //Start searching for img tag in all (divs) you found
            echo $img->src . "<br>"; //Output the information from the img's src attribute (if the found tag is <img src=""> you will get as result)

    I hope i helped you less or more.

    点赞 评论 复制链接分享