Jump to content

Why does my code for filedownload mess up?


hlstriker

Recommended Posts

Hey I have a page to download a file from a MySQL DB. I am having a problem where I will click the link to download, then it opens the download dialog box to download, and I can download it fine. Then when I click another link on my page, it messes the site up and displays all kinds of weird characters.

Here is the code to all of the page...
[code]<?
mysql_connect("localhost","user","pass");
mysql_select_db("db");


if(isset($_GET['id']))
{
$id = $_GET['id'];
$query = "SELECT homename, home1score, home2score, homefinalscore, awayname, away1score, away2score, awayfinalscore, scoretype, demoname, demotype, demosize, democontent, date, isademo, map, demodownloadtimes FROM addscore WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($homename, $home1score, $home2score, $homefinalscore, $awayname, $away1score, $away2score, $awayfinalscore, $scoretype, $demoname, $demotype, $demosize, $democontent, $date, $isademo, $map, $demodownloadtimes) = mysql_fetch_array($result);

?>
<table border='1' cellspacing='0' cellpadding='4' align='center' width='100%' bgcolor='#000000'>
<tr>
<td align='center' colspan='2'><b><? echo $homename; ?></b> verse <b><? echo $awayname; ?></b> on map <? echo $map; ?>.</td>
</tr>
<tr><td colspan='2'>
<table border='0' width='100%'><tr>
<td colspan='2' align='left'>This was a <? echo $scoretype; ?></td>
<td align='right'><? echo $date; ?></td>
</tr></table>
</tr>
<tr>
<td colspan='2' background='<? echo $bgimage; ?>'>&nbsp;</td>
</tr>
<tr>
<td align='center'><b>Round 1 Scores</b></td>
<td align='center'><b>Round 2 Scores</b></td>
</tr>
<tr>
<td align='center'><b><? echo $homename; ?></b> - <? echo $home1score; ?></td>
<td align='center'><b><? echo $homename; ?></b> - <? echo $home2score; ?></td>
</tr>
<tr>
<td align='center'><b><? echo $awayname; ?></b> - <? echo $away1score; ?></td>
<td align='center'><b><? echo $awayname; ?></b> - <? echo $away2score; ?></td>
</tr>
<tr>
<td colspan='2'>&nbsp;</td>
</tr>
<tr>
<td colspan='2' align='center'><b>Final Scores</b></td>
</tr>
<tr>
<td align='center'><b><? echo $homename; ?></b> - <b><? echo $homefinalscore; ?></b></td>
<td align='center'><b><? echo $awayname; ?></b> - <b><? echo $awayfinalscore; ?></b></td>
</tr>

<? if($isademo == "Yes")
{
$showdemosize = ("$demosize KB");
$showdemoname = ("$demoname");
$showdowntimes = ("$demodownloadtimes");
?>
<tr>
<td colspan='2'>&nbsp;</td>
</tr>
<tr>
<td colspan='2' align='center'><b>Demo Information & Download</b></td>
</tr>
<tr>
<td>File Name: <? echo $showdemoname; ?></td>
<td rowspan='3' align='center'><a href="?direc=score&page=scoreview&id=<? echo $id; ?>&download=yes">Download Demo</a><br>
</tr>
<tr>
<td>File Size: <? echo $showdemosize; ?></td>
</tr>
<tr>
<td>Download Times: <? echo $showdowntimes; ?></td>
</tr>
<?
if(($_GET['download'] == "yes") && ($_GET['id'] == "$id"))
{
header("Content-length: $demosize");
header("Content-type: $demotype");
header("Content-Disposition: attachment; filename=$demoname");
echo $democontent;

$querydemo = "SELECT demodownloadtimes FROM addscore WHERE id = '$id'";
$resultdemo = mysql_query($querydemo) or die('Error, query failed');
list($demodownloadtimes) = mysql_fetch_array($resultdemo);

$demodownloadtimes += 1;

$updatedemo = "UPDATE addscore SET demodownloadtimes='$demodownloadtimes' WHERE id = '$id'";
mysql_query($updatedemo) or die('Error, query failed');
}
} else {
?>
<tr>
<td colspan='2'>&nbsp;</td>
</tr>
<tr>
<td align='center' colspan='2'>There is not a demo to download.</td>
</tr>
<?
}
?> </table> <?
} else {
include("pages/score/showall.php");
}

mysql_close;
?>[/code]

If anyone can tell me why it is doing this, and/or how to fix it please do! Thanks!
I'm suprised that your download works at all.  You need to have the download file page be completely seperate.

Take this code:
[code]if(($_GET['download'] == "yes") && ($_GET['id'] == "$id"))
{
header("Content-length: $demosize");
header("Content-type: $demotype");
header("Content-Disposition: attachment; filename=$demoname");
echo $democontent;

$querydemo = "SELECT demodownloadtimes FROM addscore WHERE id = '$id'";
$resultdemo = mysql_query($querydemo) or die('Error, query failed');
list($demodownloadtimes) = mysql_fetch_array($resultdemo);

$demodownloadtimes += 1;

$updatedemo = "UPDATE addscore SET demodownloadtimes='$demodownloadtimes' WHERE id = '$id'";
mysql_query($updatedemo) or die('Error, query failed');
}[/code]

And make it a seperate page that you link to.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.