Help - Search - Members - Calendar
Full Version: Header Error: Cannot Modify Header Information
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
cuadronbyte
I'm having problems with running this script when I uploaded it. It works perfectly fine when I test it locally though.

Here's the code I got from a tutorial, which I edited for local testing:

===================================================

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
include "dbcon/dbcon.php";

mysql_select_db($db_name, $mydb);

$query = "SELECT `id`, `name` FROM `upload` WHERE `UName` = '".$uname."'";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "You have no uploaded files <br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
echo "<a href='download.php?id=".$id."'>".$name."</a></br>";
}
}
include "dbcon/closedb.php";
?>
</body>
</html>
<?php
if(isset($_GET['id']))
{
include "dbcon/dbcon.php";
mysql_select_db($db_name, $mydb);

$id = $_GET['id'];
$query = "SELECT `name`, `type`, `size`, `path` FROM `upload` WHERE `id` = '".$id."'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");

readfile($filePath);

include "dbcon/closedb.php";
exit;
}
?>

============================================================

this is a download script. It gives me a warning that says:

Warning: Cannot modify header information - headers already sent by (output started at /www/99k.org/c/l/a/clarenceprojex/htdocs/download.php:24) in /www/99k.org/c/l/a/clarenceprojex/htdocs/download.php on line 40

Warning: Cannot modify header information - headers already sent by (output started at /www/99k.org/c/l/a/clarenceprojex/htdocs/download.php:24) in /www/99k.org/c/l/a/clarenceprojex/htdocs/download.php on line 41

Warning: Cannot modify header information - headers already sent by (output started at /www/99k.org/c/l/a/clarenceprojex/htdocs/download.php:24) in /www/99k.org/c/l/a/clarenceprojex/htdocs/download.php on line 42

============================================================

Is there anyway I can make this work without removing the echo for the download link(s)?

Or is there a work around for this?

Thanks in advance! smile.gif
Ed
You can't output anything before calling header().

Try moving the following above your HTML:

CODE
<?php
if(isset($_GET['id']))
{
include "dbcon/dbcon.php";
mysql_select_db($db_name, $mydb);

$id = $_GET['id'];
$query = "SELECT `name`, `type`, `size`, `path` FROM `upload` WHERE `id` = '".$id."'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");

readfile($filePath);

include "dbcon/closedb.php";
exit;
}
?>


Also, you should be sanitizing your values going into the query ($id). If it's merely an integer, http://www.php.net/ctype_digit will work well.
cuadronbyte
tnx! i just made another php file to store the script and I changed the link to that file. works now. thanks again.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.