Well, I'm not sure if this belongs more in the MySQL forum or here, but it seems like I'm posting mostly PHP code so here goes.

I've searched high and low, and the best I've been able to find is a post on this user forum, so I registered to ask for your expertise. (Here is that post.)

I am trying to upload binary files to a table in MySQL. (I know that this is some resistance to doing this, but this is the route we've decided to go down for the project I'm working on.)

Here is the code for my upload form:
Code:
<html>
<form method="post" enctype="multipart/form-data" action="grabfile.php">

<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">

<input name="upload" type="submit" class="box" id="upload" value=" Upload ">
</html>
And here is the phpfile that runs when the Upload button is clicked:
Code:
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

include 'config.php';


$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed');


echo "<br>File $fileName uploaded<br>";
}

?>
This nicely puts the selected file into my DB. However, when I try to download the file, there seems not be any character encoding information. The entire file downloads--or at least, a file of the same size downloads--but nothing can open the file. JPGs and JPEGs can't be opened (on Windows or Linux), and a .doc prompts me to select the character set. But no matter which character set I choose, the file is just gibberish.

Here is the code I'm trying to download the file with:

Code:
<?php

    include 'config.php';

    if(isset($_GET['id']))
{
    $id=intval($_GET['id']);
    $query = "SELECT name, type, size, content FROM upload WHERE id=$id";
    $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);
    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    echo $content;

    exit;

}

?> 

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

<?php
    $query  = "SELECT id, name FROM upload";
   $result = mysql_query($query) or die('Error, query failed');
    if(mysql_num_rows($result) == 0)
    {
    echo "Database is empty <br>";
    }else{
    while(list($id, $name) = mysql_fetch_array($result))
{
?>
   <a href="download.php?id=<?php echo $id ?>"><?php echo $name ?></a> <br/>

<?php
    }
    }
?>

</body>

</html>

In case this helps, the columns for upload in MySQL look like this:
Code:
mysql> show full columns in upload;
+---------+-------------+-------------------+------+-----+---------+----------------+----------------------+---------+
| Field   | Type        | Collation         | Null | Key | Default | Extra          | Privileges           | Comment |
+---------+-------------+-------------------+------+-----+---------+----------------+----------------------+---------+
| id      | int(11)     | NULL              | NO   | PRI | NULL    | auto_increment | select,insert,update |         | 
| name    | varchar(30) | latin1_swedish_ci | NO   |     | NULL    |                | select,insert,update |         | 
| type    | varchar(30) | latin1_swedish_ci | NO   |     | NULL    |                | select,insert,update |         | 
| size    | int(11)     | NULL              | NO   |     | NULL    |                | select,insert,update |         | 
| content | mediumblob  | NULL              | NO   |     | NULL    |                | select,insert,update |         | 
+---------+-------------+-------------------+------+-----+---------+----------------+----------------------+---------+
Thanks in advance for any insight you can give me.

-Josh