I have a page set up to download a file to a user's hard drive. Problem is, none of the downloaded files work. They upload just fine to the server, but using my download script somehow screws them up and reduces the file size. I think it has something to do with the "Content-type" header but I'm not sure. Right now I'm using a generic "application/octet-stream", and I don't want to determine the type by the extension because it could be either 3 or 4 letters (.htm, .html for example). Using the dot wouldn't help either - this.is.a.text.file.txt. Finally, although I could store the file type in the DB when I upload them, I already have quite a few files uploaded and I don't want to have to go through them all and put in the correct file types, although I will do that if it's a last resort. Here's my download code:
<?php
require_once('config.php');
$sql = "SELECT name FROM uploads WHERE id={$_GET['id']}";
$result = mysql_query($sql) or failout('Could not query database for file name!<br>' . mysql_error());
if (!mysql_num_rows($result))
die('Sorry, but the ID you specified is invalid. Please go <a href="javascript:history.go(-1)">back</a> and try again.');
$row = mysql_fetch_array($result);
$file = $row['name'];
$file = eregi_replace(' ', '_', $file);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file");
$file = "upload/$file";
readfile($file);
?>