PDA

View Full Version : forcing download


pinkotoad
09-02-2002, 06:57 AM
I actually saw this script here, but I'm having trouble with it. What I want is it to FORCE the download.

<?

$saveName = stripslashes($HTTP_GET_VARS["name"]);
$savePath = stripslashes($HTTP_GET_VARS["path"]);

header ("Content-Type: application/octet-stream");
header ("Content-Disposition: attachment; filename=$saveName");
header ("Content-Transfer-Encoding: binary");
readfile($savePath);

?>

When i put in a query string like this:
<a href="download.php?path=mp3/backuponmyfeet.mp3&name=backuponmyfeet.mp3">

I get a page that says:
Warning: Cannot add header information - headers already sent by (output started at /home/ainsoftc/public_html/glen/download.php:1) in /home/ainsoftc/public_html/glen/download.php on line 6

Warning: Cannot add header information - headers already sent by (output started at /home/ainsoftc/public_html/glen/download.php:1) in /home/ainsoftc/public_html/glen/download.php on line 7

Thanks for any help.

freakysid
09-04-2002, 04:00 PM
Your script must not have sent any HTML to the client before calling header(). This is because (unless you are using output buffering) once your script starts to output anything, php will send off the HTTP headers.

Remember that PHP is an embedded scripting language. It is embedded into HTML. So anything before the opening <?php tag will be output as standard HTML (even simple whitespace will be forwarded to the client broswer) forcing php to send the HTTP headers.

So no whitespace before the opening <?php tag! :p

pinkotoad
09-07-2002, 02:26 AM
Somehow the php file got encoded at utf-8. That was causing the problem. :mad: