Dexty
Dexty

Reputation: 1472

Download all images from a single webpage

I'm experimenting with PHP, and I've just asked myself a question. How can I download all images from a single website? Let's say that I wanted and images from cnn.com or any other website. Is there any easy way I can download all the images using PHP, and save them on my local computer (or FTP server).

Any help would be appreciated.

Upvotes: 2

Views: 1532

Answers (2)

str
str

Reputation: 44969

Not very easy, but not that hard:

Request the page using Curl, parse the HTML using DOMXpath and look for the src attributes of the images. Then download the images via Curl by adding the full http address to the image path/name.

Upvotes: 4

alex
alex

Reputation: 490153

If you wanted just all download all images referenced by img elements.

  1. Request the remote file with something like cURL.
  2. Use a DOM parser (such as DOMDocument) to get all img element.
  3. Iterate over the img elements and use cURL (or similar) to download each image (check the src attribute).

Upvotes: 0

Related Questions