Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download all images from a website?

Tags:

php

curl

I want to download multiple images from the following website: www.bbc.co.uk I want to do it by using PHP cURL, can someone help lead me in the right direction?

It would be nice to download all the images in one shot, but if someone can help me download maybe download just 1 or a bunch that would be great!

Edit: it would be a good idea to show what I have tried:

<?php
$image_url = "www.bbc.co.uk";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

$image = curl_exec($ch);
curl_close($ch);

// output to browser
header("Content-type: image/jpeg");
print $image;
?>

For some reason it is not working. It is to be noted I am an absolute amatuer at PHP and programming in general.

like image 670
Navee Avatar asked Nov 30 '25 03:11

Navee


1 Answers

The above code you pasted isn't doing what you think it is.

$image = curl_exec($ch);

The $image variable doesn't contain any image, it actually contains the entire HTML of that webpage as a string.

If you replace

// output to browser
header("Content-type: image/jpeg");
print $image;

with:

var_dump($image);

You will see the html.

Something like this:

curl output

Try to find the actual champion image source and parse it accordingly

like image 155
Timmetje Avatar answered Dec 02 '25 20:12

Timmetje