tessguefen
8/5/2015 - 4:53 PM

Grab a list of images, and download // aka.. grab all images being used in miva and delete the other ones.

Grab a list of images, and download // aka.. grab all images being used in miva and delete the other ones.

<?php
function readCSV($csvFile){
  $file_handle = fopen($csvFile, 'r');
  while (!feof($file_handle) ) {
    $line_of_text[] = fgetcsv($file_handle, 0);
  }
  fclose($file_handle);
  return $line_of_text;
}

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);
}

$products = readCSV('products-test.csv');
// make CSV something like..
// ProductCode ProductName active ImageURL[this would be main]
// if your client is using more than 1 image.. this will need to be modified.
//make sure this file is inside your /00000001-TEST folder so the images go in here.
echo '<table>';
foreach ($products as $p) {
  $code = $p[0];
  $name = $p[1];
  $imgurl = $p[3];

  $url = "http://www.belmonttv.com/mm5/$imgurl";
  $imgName = basename($url);

  echo '<tr>';
    echo '<td>'.$code.'</td>';
    echo '<td>'.$url.'</td>';
  echo '</tr>';

  $file = grab_image($url,$imgName);
}
echo '</table>';