Page 1 of 1

Please help me with my download script

Posted: Tue Sep 13, 2005 12:58 pm
by duyhung85
Hello,
I am a newbie in Web programming and I want to make a download script so people could download my file.exe.
Here is the script download.php:

Code: Select all

<?php
$dir="/download/file/";
if (isset($_REQUEST['file'])) {
     $file=$dir.$_REQUEST['file'];
     header("Content-type: text/plain");
     header("Content-Transfer-Encoding: Binary");
     header("Content-length: ".filesize($file));
     header("Content-disposition: attachment; filename=\"".basename($file)."\"");
     readfile("$file");
} else {
     echo "No file selected";
}
?>
It appears to encounter the following errors:

Code: Select all

Warning: Warning: Cannot modify header information - headers already sent by (output started at D:\My Website\download.php:10) in D:\My Website\download.php on line 14
The errors appear in all line that have header code.

And here is my index.htm:

Code: Select all

<html>
<body>
<a href= "../download.php[b]?file=file.exe[/b]" ><font color="#00CC00">Download soft</font></a>
</body>
</html>
Anyone please explain me what's wrong with the code and how to solve it

Thanks alot,

Posted: Tue Sep 13, 2005 1:02 pm
by Burrito
are you outputting anything to the client above your header()'s?

Posted: Tue Sep 13, 2005 1:45 pm
by duyhung85
Thank you for replying,
Burrito wrote:are you outputting anything to the client above your header()'s?
I am sorry, i don't really understand what you mean, can you explain a bit , i'm quite new to PHP. The download.php includes the php codes that i posted and some of the normal html code, you know, like this:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>download.php</title>
</head>

<body>
...

Posted: Tue Sep 13, 2005 1:51 pm
by Burrito
you can not output anything to the client above your header tags...not even whitespace.

for example:

Code: Select all

<? 
echo "hello";
header(...);
?>
// would fail

Code: Select all

<?
header(...)
// would fail (note the carriage return at the top)
?>

Code: Select all

<?
header(...);
//will work
?>
edit: I guess you can't note the carriage return at the top...but pretend there's one there 8O

Posted: Tue Sep 13, 2005 2:40 pm
by duyhung85
Thank you, mate. I solved the problems :D