PHP script to download sequence of files from website

24 Dec

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";
}
?>
Advertisements

8 Responses to “PHP script to download sequence of files from website”

  1. Anabaric April 3, 2012 at 7:08 am #

    Ah, just what i was looking for!

  2. pawan November 9, 2012 at 3:07 pm #

    this code is not working for picture file ..why??

    • sumanrs November 13, 2012 at 5:02 pm #

      Can you describe the problem?

  3. sanju January 25, 2013 at 5:35 am #

    This script is not working why?

    • sumanrs January 31, 2013 at 4:24 pm #

      Can you tell me what error you are encountering?

  4. Bilal March 23, 2013 at 1:39 pm #

    Ahhh sooperb…it really worked gr88

  5. Archie April 2, 2013 at 10:32 pm #

    can you tell me how its work, for no destination folder?

  6. allthebest April 3, 2014 at 12:52 pm #

    instead of <? , write <?php

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: