0

I want to download about 200 images from an URL. For instance: Fetch www.web.com/images/001.png and download, fetch www.web.com/images/002.png and download and finish in 200.png.

I've read Grab/download images from multiple pages using php preg_match_all & cURL but I don't know how to modify the PHP to do that thing.

I'll be very appreciated if you can help me. Thank you so much

Community
  • 1
  • 1
jd250
  • 1
  • 1

3 Answers3

1

The simplest way that comes to mind, is to create a for loop, which counts from 1 to 200 and for every count, it then does a request for the image and saves it to the disk (I assume you want to save the image to your disk). Working example can be found at the bottom.


Let's start by settings some variables:

$baseUrl = 'http://www.web.com/';
$localDirectory = 'downloaded_images/';
$maxImageNumber = 200;

The first variable $baseUrl defines where the images will be loaded from. The second defines your local directory in which the images will be saved. Please make sure that this directory exists before running the code, because it will not be automatically generated. The last variable, $maxImageNumber stores the largest image number, which we will need for the for loop.

After setting the variables, we can write the for loop. Separated by semicolons ; there are three parts in the brackets. First one being the starting point of the number we will be counting upwards. The second makes sure we don't go over our limit and the last one just states that the number will be counted up, using the ++ shorthand.

for($imageNumber = 1; $imageNumber <= $maxImageNumber; $imageNumber++) {
    // code goes here
}

Inside this for loop we can now generate our file name and download/save the image to the disk. The first line uses the function str_pad() to add leading 0s to the image file (just like in your example) and then adds the extension .png to the name. This allows us to reuse the file name for loading as well as for saving. Second line loads the image with the function file_get_contents() by combining the base and the image file name. In the last line we use file_put_contents() to save the $fileData, we loaded just before, to the disk (using the local directory and the image file name).

$imageFileName = str_pad($imageNumber, 3, '0', STR_PAD_LEFT) . '.png';
$fileData = file_get_contents($baseUrl . $imageFileName);
file_put_contents($localDirectory . $imageFileName, $fileData);

The complete code should look like this:

<?php

$baseUrl = 'http://www.web.com/images/';
$localDirectory = 'downloaded_images/';
$maxImageNumber = 10;

for($imageNumber = 1; $imageNumber <= $maxImageNumber; $imageNumber++) {
    $imageFileName = str_pad($imageNumber, 3, '0', STR_PAD_LEFT) . '.png';
    $fileData = file_get_contents($baseUrl . $imageFileName);

    file_put_contents($localDirectory . $imageFileName, $fileData);
}

In the unlikely event that this all should not work, there are a few things you can try

  • check that the $localDirectory exists and is writable.
  • if the file_get_contents() should not work, you can use curl instead
  • make sure the computer this code is executed on can access the remote server
Community
  • 1
  • 1
0

Thank you so much but imagine that pics are in www.web.com/image_0001_big.png and www.web.com/image_002_big.png and so on... (last will be web.com/image_0200_big.png)

This doesn't work (i don't know why)

    <?php

$baseUrl = 'http://www.web.com/images';
$localDirectory = 'downloaded_images/';
$maxImageNumber = 200;
$input='image_';

for($imageNumber = 001; $imageNumber <= $maxImageNumber; $imageNumber++) {
    $imageFileName = str_pad($input, $imageNumber, 4, '_big', STR_PAD_LEFT) . '.png';
    $fileData = file_get_contents($baseUrl . $imageFileName);

    file_put_contents($localDirectory . $imageFileName, $fileData);
}

Thanks. You're amazing! Thanks for the help! I'm learning PHP and I'm a beginner yet:(

My error:

Warning: str_pad() expects at most 4 parameters, 5 given in C:\xampp\htdocs\auto.php on line 9

Warning: file_get_contents(http://www.web.com/images/.png): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\xampp\htdocs\auto.php on line 10

jd250
  • 1
  • 1
0

I've found a semi-solution:_

    <?php

$baseUrl = 'http://web.com/images';
$localDirectory = 'downloaded_images/';
$maxImageNumber = 400;
$input='image0';
$input2='big';
$extension ='.png';

for($imageNumber = 100; $imageNumber <= $maxImageNumber; $imageNumber++) {
    $fileData = file_get_contents($baseUrl . $input . $imageNumber . $input2 . $extension);
    file_put_contents($localDirectory . $imageNumber, $fileData);
}

?>

The problem is when... 0001 0009 0010 !! I want to tell php "hey, always 4 digits"

jd250
  • 1
  • 1