Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8547629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:18:24+00:00 2026-06-11T13:18:24+00:00

I am trying to create a PHP function that downloads images from a webpage

  • 0

I am trying to create a PHP function that downloads images from a webpage that you put in as a parameter. However, the webpage itself though is a kind of gallery which only has very small thumbnail versions of the images, each linking directly to the larger full jpeg images that I want to download to my local computer. So the images will not downloaded directly from the webpage itself that I put into the function, but rather from the individual links to these jpeg image files on the webpage.

So for example:

www.somesite.com/galleryfullofimages/

is the location of the image gallery,

and each jpeg image file from the gallery that I want is then located at something like:

www.somesite.com/galleryfullofimages/images/01.jpg
www.somesite.com/galleryfullofimages/images/02.jpg
www.somesite.com/galleryfullofimages/images/03.jpg

What I’ve been trying to do so far is to use the file_get_contents function to get the full html of the webpage as a string, and then try to isolate all of the <a href="images/01.jpg"> elements inside the quotes and put them inside of an array. Then use this array to locate each image and download them all with a loop.

this is what I have done so far:

<?php

$link = "http://www.somesite.com/galleryfullofimages/";
$contents = file_get_contents($link);

$results = preg_split('/<a href="[^"]*"/', $contents);

?>

But I am stuck at this point. I am also totally new to regular expressions, which as you can see I tried to use. How can I isolate each image link and then download the image? Or is there a better way of doing this altogether? I have also read about using cURL. But I can’t seem to implement that either.

I hope this all makes sense. Any help will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:18:25+00:00Added an answer on June 11, 2026 at 1:18 pm

    This is commonly known as “scraping” a website. You already are retrieving the markup for the page, so you are off to a good start.

    Here’s what you need to do next:

    <?php
    // Load the retrieved markup into a DOM object using PHP's
    // DOMDocument::loadHTML method.
        $docObj = new DOMDocument();
        $docObj->loadHTML($contents);
    
    // Create a XPath object.
        $xpathObj = new DOMXpath($docObj);
    
    // Query for all a tags. You can get very creative here, depending on your
    // understanding of XPath. For example, you could change the query to just
    // return the href attribute directly. This code returns all anchor tags in
    // the page, if the href attribute ends in ".jpg".
        $elements = $xpathObj->query('//a[ends-with(@href,".jpg")]');
    
    // Process the discovered image URL's. You could use cURL for this,
    // or file_get_contents again (since your host has allow_url_fopen enabled)
    // to fetch the image directly and then store it locally.
        foreach ($elements as $domNode)
        {
            $url = $domNode->getAttribute('href');
        }
    ?>
    

    DOMDocument::loadHTML
    XPath
    XPath::query
    allow_url_fopen

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a PHP function that will echo info from a
Basically, I'm trying to create a function in php that would give me the
I'm trying to create a PHP file that calls a function in another file.
In trying to create a simple PHP PDO update function that if the field
Im trying to create a php function that converts a string date to a
I am trying to create a function that would parse php code and return
I am trying to create a function that deletes a user from Constant Contact.
I am trying to create a PHP function that Ciphers a string using a
I am trying to create a php file that adds a user and create
I am trying to create a php file that would scan a directory, make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.