Hi,
I have written you a script that does what you need.
There are a number of requirements, primarily - mod_rewrite using .htaccess.
Step 1, set up rewrite rule
Open your text editor, and put the following lines into a new text file; saving it out as .htaccess and placing the file in your document root:
RewriteEngine On
RewriteRule ^(downloads)/([a-zA-Z.0-9]+)$ /download.php?action=1&file=$2 [L]
This will redirect all requests like this:
http://www.your-domain.com/downloads/something.zip.html to the download script. Note! Change the (downloads) part of the RewriteRule to change the /downloads/ part of the URL you want.
Step 2, plug in download summary template
Next, lets set up the download summary page - open a new text file and put the following in it:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Download file </TITLE>
<!-- This refresh tag will automatically offer the forced download after 30 seconds -->
<META HTTP-EQUIV="refresh" content="30;URL=/download.php?action=2&file={FILE_TO_DOWNLOAD_LINK}">
</HEAD>
<BODY>
<h2> Download the file: <font color=red>{FILE_TO_DOWNLOAD}</font> </h2>
<p> If you do not want to wait, please <a href="/download.php?action=2&file={FILE_TO_DOWNLOAD_LINK}">click here to download now</a>. </p>
</BODY>
</HTML>
Save this as "download-summary.tpl" and put it in your root directory too.
Step 3, configure and copy the main download script
Next, the main download script. Simply copy and paste the following into a file called "download.php" and put it in your document root too:
<?PHP
# Download script with rewrite.
# 21.11.2005
# Author - Brit
# PHPBuilder thread ID - 10312677
# REQUIREMENTS: You require mod_rewrite via .htaccess
# VARIABLES: ?action= "1=Summary display, 2=Download" & file= "filename.zip.html"
/* The following should be altered as required. */
/* 1. The base location of the files you want to offer for download */
DEFINE ("FILESTORE_BASE","/path/to/your/docroot");
/* 2. The location of your download summary template */
DEFINE ("TEMPLATE_BASE","/path/to/your/docroot");
/* 3. The page we will push users to if they specify a file that doesn't exist */
DEFINE ("FILE_NOT_EXIST","/error.php");
/* 4. The name of the download summary template */
DEFINE ("FILE_DOWNLOAD_TEMPLATE","download-summary.tpl");
/*************************************************************************************/
/* The rest of the script which you shouldn't need to change, happens below this break
/*************************************************************************************/
/*************************/
/* Force a download for user
/*************************/
function doDirectDownload($fileToDownload)
{
/* Start by making sure the file actually exists */
if(checkExistenceOfFile($fileToDownload))
{
/* If your web server has output compression enabled (quite common) this needs to be */
/* disabled in order for MSIE to obey our Content Disposition header */
if(ini_get('zlib.output_compression'))
{
ini_set('zlib.output_compression', 'Off');
}
/* Get the file size */
$fileSize = filesize(sprintf("%s/%s",FILESTORE_BASE,returnActualFilename($fileToDownload)));
/* Set the timelimit to zero, to prevent timeouts */
set_time_limit(0);
/* Set the headers required to force a download. Explanation after each for reference */
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".returnActualFilename($fileToDownload)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fileSize);
/* Read in the original file and present dialog to user */
readfile(FILESTORE_BASE."/".returnActualFilename($fileToDownload)) or die("File not found.");
}
else
{
exit;
}
}
/*************************/
/* Return the actual file name based on _GET vars
/*************************/
function returnActualFilename($fileToDownload)
{
/* NOTE: this relies on you ensuring you are using the FILENAME.EXTENSION.html form! */
return substr($fileToDownload,0,-5);
}
/*************************/
/* Check to see whether a file exists
/*************************/
function checkExistenceOfFile($fileToDownload)
{
/* See whether the specified file is available, and can be read. If not, return false. */
if(!is_file(sprintf("%s/%s",FILESTORE_BASE,returnActualFilename($fileToDownload))) || !is_readable(sprintf("%s/%s",FILESTORE_BASE,returnActualFilename($fileToDownload))))
{
header(sprintf("Location:%s?error=404&file=%s",FILE_NOT_EXIST,returnActualFilename($fileToDownload)));
exit;
}
/* Otherwise, return true */
else
{
return true;
}
}
/*************************/
/* Show initial download options
/*************************/
function displayFileForDownloadSummary($fileToDownload)
{
/* Check to make sure the file exists and can be read */
if(checkExistenceOfFile($fileToDownload))
{
/* First, open the summary template and get it into a string we can work with */
$summary_template_file = sprintf("%s/%s",TEMPLATE_BASE,FILE_DOWNLOAD_TEMPLATE);
$summary_template = fopen($summary_template_file,"r");
$summary_template_contents = fread($summary_template, filesize($summary_template_file));
fclose($summary_template);
/* Now replace relevant spacers with file data */
$summary_template_contents = preg_replace('/{FILE_TO_DOWNLOAD}/', returnActualFilename($fileToDownload), $summary_template_contents);
$summary_template_contents = preg_replace('/{FILE_TO_DOWNLOAD_LINK}/', $fileToDownload, $summary_template_contents);
/* Render summary with relevant content to user */
echo $summary_template_contents;
}
else
{
exit;
}
}
/*************************/
/* Start relevant function based on _GET variables
/*************************/
function startDownloadProcess()
{
if(!isset($_GET["action"]) || strlen($_GET["action"]) > 1 || !isset($_GET["file"]))
{
/* Chances are, someone is calling the script direct, or doing something odd, so exit */
exit;
}
else
{
switch($_GET["action"])
{
case 1: /* Display the file summary page */
displayFileForDownloadSummary($_GET["file"]);
break;
case 2: /* Force file download */
doDirectDownload($_GET["file"]);
break;
default:
exit;
break;
}
}
}
startDownloadProcess();
?>
Step 4, use!
Thats it - now just call the script like this, in the links to the files you want to download:
<a href="/downloads/something.zip.html">Download</a>
Note! You have to use the right file name for the links, specifically:
<filename>.<extension>.html
Regards,
Brit.