PHP script to download sequence of files from website
I often find myself having to read documents split into multiple files, and it becomes a pain to bookmark and go back to them later. I prefer downloading them (if I can) to my local hard drive and place them into a folder. Sometimes the documents are numbered sequentially, which is really helpful since I can use a PHP script to just automate the download.
This is the PHP script that I wrote recently to automate some of my downloads. Note that there are two pre-requisites to be able to run this script:
- PHP command-line program (php5-cli) has to be installed
- file_get_contents() has to allow remote URL fetches
Without much ado, here is the script.
<?
/*
* THIS SCRIPT DOWNLOADS A SEQUENCE OF FILES FROM THE WEB
* TO THE LOCAL DRIVE.
* Released under GPL.
* Author: Suman Srinivasan, Dec 24, 2010
*/
/* Replace the parameters below with the names you need.
* The "{x}" part of the string pattern will be replaced by the
* number in the script. */
/* The initial parameters */
// Source URL pattern
$sourceURLOriginal = "http://www.somewebsite.com/document{x}.pdf";
// Destination folder
$destinationFolder = ".";
// Destination file name pattern
$destinationFileNameOriginal = "doc{x}.pdf";
// Start number
$start = 1;
// End number
$end = 10;
// From start to end
for ($i=$start; $i<=$end; $i++) {
// Replace source URL parameter with number
$sourceURL = str_replace("{x}", $i, $sourceURLOriginal);
// Destination file name
$destinationFile = $destinationFolder . "/" .
str_replace("{x}", $i, $destinationFileNameOriginal);
// Read from URL, write to file
file_put_contents($destinationFile,
file_get_contents($sourceURL)
);
// Output progress
echo "File #$i complete\n";
}
?>
Categories: php

Ah, just what i was looking for!