Web dev

Bulk images downloader

If you want to know how to bulk download images from a list of urls, you are in the right place :-).


For donwload images with the code below, you need to have a table in a database (I used mysql notation) with the list of image url.

  • You should connect to database (not explained in the article);
  • extract an image’s URL;
  • download the image in a specific directory.

If for example you have a table with only 2 fields: image id (id), image url (url), will be very easy.

PHP Code

The code is self explained, the first part is for settings, then there is a loop between the image’s urls with save commands, at the end will be the result of the bulk downlad.

/* settings */
//folder for images saving
$saveDir = "/download/";

//database name table
$table = "image_save";

//database connection
$con= new mysqli("localhost","my_user","my_password","my_database");

//start and end table row to prevent timeout or hard server work
$start = "1";
$end = "200";
/* end of settings */

//query for fetching the image's urls
$sql = "SELECT * FROM ".$table . " ORDER BY id DESC LIMIT ".$start . ",".$end . " ";
$res = $con->query($sql);

$count = 0;          //this is for count the total row fetched
$notify = "";        //this is for seeing result or errors

while ($row = $res->fetch_array(MYSQLI_ASSOC)){
$url = $row['url'];
$dest = $saveDir . clean($row['url']) . ".jpg";

if (!file_exists($dest)){   //prevent file overwriting

   if (copy($url, $dest)){
      $notify.= "image saved: ". $row['url'];
      $notify.= "ERROR saving image: ". $row['url'];

   $notify.= "image already exists: ". $row['url'];}

//output result
echo "TOTAL IMAGE SAVED: " .$count ."\n";
echo $notify;



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.