Skip to content

Instantly share code, notes, and snippets.

@DfKimera
Created December 28, 2012 16:57
Show Gist options
  • Select an option

  • Save DfKimera/4399675 to your computer and use it in GitHub Desktop.

Select an option

Save DfKimera/4399675 to your computer and use it in GitHub Desktop.
<?php
$filelistPath = "filelist.txt";
$baseURL = "http://url-with-images.com/";
// ------------
$filelist = file($filelistPath);
function downloadFile($url, $target) {
global $baseURL;
$ch = curl_init();
$fp = fopen($target, "w+");
$timeout = 60;
curl_setopt($ch, CURLOPT_URL, $baseURL . $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
}
$ts = microtime(true);
$zipPath = "zipped/".date("Y-m-d_H:i:s_").uniqid().".zip";
$zip = new ZipArchive();
if($zip->open($zipPath, ZIPARCHIVE::CREATE) !== true) {
die("Failed to create zip file");
} else {
echo "\nZip file created: $zipPath";
}
echo "\nDownloading ".sizeof($filelist)." files...\n";
$num = 0;
foreach($filelist as $file) {
$file = trim($file);
echo "\nFile: {$file}... ";
$localFile = "downloaded/".basename($file);
downloadFile($file, $localFile);
echo "DOWNLOADED!";
$zip->addFile($localFile);
echo " ...ZIPPED!";
$num++;
}
$zip->close();
$te = microtime(true);
$tt = $te - $ts;
$timeTotal = sprintf("%.3f", $tt / 1000);
die("\n\nCompleted! Downloaded {$num} files in {$timeTotal} seconds");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment