Jump to content


Photo

Why does my code for filedownload mess up?


  • Please log in to reply
2 replies to this topic

#1 hlstriker

hlstriker
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts

Posted 14 August 2006 - 11:34 PM

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...
<?
	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;
?>

If anyone can tell me why it is doing this, and/or how to fix it please do! Thanks!

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 14 August 2006 - 11:42 PM

That doesnt help at all. can we see an example?

#3 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 14 August 2006 - 11:58 PM

I'm suprised that your download works at all.  You need to have the download file page be completely seperate.

Take this 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');
			}

And make it a seperate page that you link to.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users