Ricard's code gists


cURL files and download them with PHP

It looks through a set of URL’s (usually numeric) and downloads each image file to disk

<?php

function grab_image($url,$saveto){
  $ch = curl_init ($url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  $raw=curl_exec($ch);
  curl_close ($ch);
  if(file_exists($saveto)){
      unlink($saveto);
  }
  $fp = fopen($saveto,'x');
  fwrite($fp, $raw);
  fclose($fp);
}

for ($i=1; $i <= 36; $i++) {

  $extension = "jpg";
  $num_padded = sprintf("%02d", $i);
  $num_padded = $i;

  $url = "https://SITE.COM".$num_padded.".".$extension;

  grab_image($url,"downloads/image-"  . $num_padded .".".$extension);  # code...
}