2

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 example.com 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

<?php
$html = file_get_contents('http://www.tgo-tv.net');
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

Community
  • 1
  • 1
  • You have to scrape the site, get the urls, then look at this answer: http://stackoverflow.com/questions/3938534/download-file-to-server-from-url Don't want to be rude, but I didn't know how to do this either, but with a simple google search I found an answer. A small suggestion for your next problems – Nytrix Apr 24 '17 at 22:43
  • @Nytrix i really in need of it. i would really appreciate it if you make it and post it here as an answer. i don't know much in php. and yes you may say learn php first i am learning but right now i am in need of it like in urgent – wesley --------------------- Apr 24 '17 at 22:45
  • *No.* If you *need* something over your knowledge, you hire someone who can do it. SO is **not** a free coding service, *urgency* is never a good reason to post something on here... I gave you a post where your problem *is* answered. – Nytrix Apr 24 '17 at 22:47
  • @Nytrix you won't be doing it for ever for me , it's just simple help i asked – wesley --------------------- Apr 24 '17 at 22:49
  • I gave you the answer on how to save a file from an URL, what more do you need? – Nytrix Apr 24 '17 at 22:51

2 Answers2

3

If you want something generic, you can use:

<?php
    $the_site = "http://somesite.com";
    $the_tag = "div"; #
    $the_class = "images";

    $html = file_get_contents($the_site);
    libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);

    foreach ($xpath->query('//'.$the_tag.'[contains(@class,"'.$the_class.'")]/img') as $item) {

        $img_src =  $item->getAttribute('src');
        print $img_src."\n";

    }

Usage:

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 = "https://stackoverflow.com/questions/23674744/what-is-the-equivalent-of-python-any-and-all-functions-in-javascript";
$the_tag = "div"; #
$the_class = "gravatar-wrapper-32";

Output:

https://www.gravatar.com/avatar/67d8ca039ee1ffd5c6db0d29aeb4b168?s=32&d=identicon&r=PG
https://www.gravatar.com/avatar/24da669dda96b6f17a802bdb7f6d429f?s=32&d=identicon&r=PG
https://www.gravatar.com/avatar/24780fb6df85a943c7aea0402c843737?s=32&d=identicon&r=PG
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

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:

<?php
require_once("./simple_html_dom.php");
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="www.example.com/cat.png"> you will get www.example.com/cat.png as result)
    }
}
?>

I hope i helped you less or more.

XTard
  • 56
  • 8