Please help me with my download script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
duyhung85
Forum Newbie
Posts: 3
Joined: Sun Jun 19, 2005 12:43 pm

Please help me with my download script

Post 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,
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

are you outputting anything to the client above your header()'s?
duyhung85
Forum Newbie
Posts: 3
Joined: Sun Jun 19, 2005 12:43 pm

Post 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>
...
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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
duyhung85
Forum Newbie
Posts: 3
Joined: Sun Jun 19, 2005 12:43 pm

Post by duyhung85 »

Thank you, mate. I solved the problems :D
Post Reply