Click to See Complete Forum and Search --> : A Complete Download Script for .exe files i.e. download.php?


ramram
04-21-2006, 12:16 PM
I want to make one download.php file. So if someone wants to download a file, the URL address would be somewhat like this

http://www.domain.com/download.php?filename

instead of

http://www.domain.com/directory/filename.exe

I have no idea what so ever to make such file so pleas post all comments a long with the code. I really appreciate your help.

aaronbdavis
04-21-2006, 01:27 PM
One: What is wrong with the way you don't want to use?
Two: The way you do want to use is a security risk as they could download anyfile from your server. The usual example of this would be to request /download.php?filename=/usr/etc/.htpasswd which would give them your Apache password file, thus giving them access to anywhere on your site.

GaryS
04-21-2006, 05:26 PM
One approach would be to store (in a database) a list of "codes" corresponding to the files - helps with the security concerns. So the request might be for:

http://www.domain.com/download.php?filename=peter

and you could look up "peter" in your database and find (a) whether it exists and (assuming that it does exist) (b) that it corresponds to (say) files/downloads/peter.pdf

Assuming you know the mime-type (e.g., application-pdf) of the file (the mime type could be stored in the database), you're ready to go:

$file=file_get_contents($fullPath);

$contentLength=strlen($file);

//Override the MIME type for certain browsers
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or
strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
$mimeType = 'application/x-download';
}

// Tell the browser it's a file for downloading
header('content-disposition: attachment; filename=' . $filename );
header('content-type: ' . $mimeType);
header('content-length: ' . $contentLength);

// Display the file
echo $file;

Hope that helps.