Forum Moderators: coopster
<?php
$filename = $_GET['file'];
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if( $filename == "" )
{
echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";
exit;
};
switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>
I'm activating the script like so:
<img src="Decor_PDF.png" onclick="download.php?=file.pdf">
However, when I click on the png image, nothing at all happens. I've tried several scripts, all with the same result. The script and the file are both in the root directory of the server. Any clues?
You will probably hate this. :-) It's not a PHP problem, it's in your HTML.
This is because an image doesn't inherently "do anything" or react to user input. You can try to force it to do something, assigning an onClick action to the image using Javascript, (which you've tried to do, but as you see, doesn't work like that) but this complicates a simple task. The solution: use the natural element for navigation (your "click",) the anchor:
<a href="download.php?file=file.pdf"><img src="Decor_PDF.png"></a>
Note also, the correction to the info after ?. In your initial link, you don't have a "name" for what's expected in get:
$filename = $_GET['file'];
You'll have to style the image as border:none or use the img attribute border="0" because now it's the content of a hyperlink, and hyperlinks have an underline for text and border on other elements by default. The style option is better because "border" is not a valid image attribute in 4.01 strict or XHTML (i.e., the page won't validate.)
One last bit of info, all those file types in the switch are really unnecessary if you want to force download for any object. just delete the entire switch and put this in it's place:
$ctype="application/force-download";
or even
$ctype="bad/type";
The idea is that you are intentionally feeding the browser an unrecognized content type, so when it gets that header it's going to force a download because it doesn't know how to handle it. There does exist the possibility that, say, for a .pdf, if you give it the header application/pdf, instead of downloading it will just display.