PDA

View Full Version : Force Download?



Eclyps19
08-16-2006, 04:44 PM
Hey guys. I need help. A client wants vCards or their employees that people can click and have outlook open. Unfortunately, when I click the current vCard link, it just opens as text in the browser. Is there any way I can force a download so they can choose "open with"? Thanks =)

//Edit: in case you need to see what's going on, www.kaplanbond.com/atts.html
click an attorney and try to open one of their vCards.

Twey
08-16-2006, 04:46 PM
Please search the forum before posting.

jscheuer1
08-17-2006, 06:18 AM
I think you just missed an opportunity to be both more informative and less polite at the same time. It's really a cr_p shoot, depending upon the browser and a user's configuration. A situation analogous to speed dating and expecting to control your new partner's behavior. If you are really good, you might be able to part of the time. Oh, and do search previous posts. If you know the user's browser and settings and can count on this information it may be doable.

Eclyps19
08-21-2006, 01:54 PM
I think you just missed an opportunity to be both more informative and less polite at the same time.

What? Are you saying that I should be less polite and more informative?

Anyways, sorry I didn't do a search first. My fault. But I DID find this PHP script, but I can't quite get it to run properly.


<?php

$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" )
{
echo "<html><title>Error</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title>Error</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 "vcf": $ctype="application/vcard"; 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();

?>



First of all, I'm not sure how to format the filepath. but even without the path, it should tell me file not found, correct? when I go to http://www.kaplanbond.com/test.html and click the link that links to http://www.kaplanbond.com/test1.php?David%20B%20%20Kaplan.vcf it tells me the file was not specified.

If you need more specifics just ask and I'd be happy to reply with what you need. Thanks in advance.

Twey
08-21-2006, 05:01 PM
What? Are you saying that I should be less polite and more informative?He meant me :)

Whatever you do, don't use that script. It is shockingly insecure, and will allow anyone access to read any file the user your scripts run as can read! For example, your server's /etc/passwd (http://www.kaplanbond.com/test1.php?file=/etc/passwd). Luckily your host is smart enough to use shadowing, but that's still a serious problem.

Eclyps19
08-22-2006, 05:27 AM
what about forcing the file to be a certain name? Instead of reading it from the link.

Twey
08-22-2006, 03:41 PM
That's a much better idea. Still, there are easier ways to obtain the effect you desire, such as mwinter's .htaccess idea.

tacmig99
08-23-2006, 03:24 AM
And this is what Twey is refering to I think.

AddType application/octet-stream zip
That would force any and all .zip files to be downloaded without use of the right click save as feature, make sure you don't add a period before the file extension.