PHP CLI Download List Script
Something I wrote to download a list of resource.
This doesn’t use forking since I wrote this on a windows machine, so resources will have to be downloaded 1 at a time. Files are automatically overwritten if they exists. The destination directory is automatically created if one does not exists.
<?php
/* incoming data */
$url = $_SERVER['argv'][1];
$dir = $_SERVER['argv'][2];
$min = (int)$_SERVER['argv'][3];
$max = (int)$_SERVER['argv'][4];
$len = isset($_SERVER['argv'][5]) ? (int)$_SERVER['argv'][5] : 0;
if (count($_SERVER['argv']) < 5) {
output('SYNTAX: php download.php <url> <dir> <min> <max> [len]');
output(' <url> - url to download resource, use %i to indicate the index');
output(' <dir> - directory to save the resource');
output(' <min> - starting index value');
output(' <max> - ending index value');
output(' [len] - optional length value for padding zeroes', true);
}
/* validate data */
if ($min < 0) output('ERROR: <min> must be greater than 0', true);
if ($max < 0) output('ERROR: <max> must be greater than 0', true);
if ($max < $min) output('ERROR: <max> cannot be less than <min>', true);
/* initialize */
if (!file_exists($dir)) mkdir($dir);
if (!preg_match('/\/$/', $dir)) $dir .= '/';
/* execute */
for ($i = $min; $i <= $max; $i++) {
$newi = str_pad($i, $len, '0', STR_PAD_LEFT);
$newurl = preg_replace('/%i/', $newi, $url);
$info = pathinfo($newurl);
$file = $info['basename'];
$data = @file_get_contents($newurl);
if ($data === false) output('Failed to download file, "' . $file . '".');
elseif (@file_put_contents($dir . $file, $data) === false) output('Failed to write file, "' . $file . '".');
else {
$size = strlen($data);
output('Saved file, "' . $file . '" (' . $size . ' bytes).');
}
}
/* functions */
function output($msg = '', $end = false) {
echo $msg . "\r\n";
if ($end) die;
}
?>