Home > php > PHP script to download sequence of files from website

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
  1. Anabaric
    April 3, 2012 at 7:08 am | #1

    Ah, just what i was looking for!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.