Configuration Problem for Binary Downloads from MYSQL?

software development

#1

Well, it seems that I spoke too soon ;-).

I’ve managed to get binary files into my DB. (see This Thread.) Trouble is I can’t get an “openable” file out of the db. The file downloads just fine, the OS tries to open it with the appropriate application, but then the application indicates that there is some kind of encoding problem. For example, the image viewer won’t open a jpg. Instead it gives a message that says “Error interpreting JPEG image file (Not a JPEG file: starts with 0x0a 0xff)”

Here is the form:

[code]

[/code]

Here is the upload code (grabfile.php):

[code]<?php
include ‘config.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);

$fileName = addslashes($fileName);

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

mysql_query($query) or die(‘Error, query failed’);

echo “
File $fileName uploaded
”;
}
echo “Download Page”;
?>[/code]

And here is the download code:

[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;

}

?>

Download File From MySQL <?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
"; }else{ while(list($id, $name) = mysql_fetch_array($result)) { ?>

<?php echo $name ?>

<?php } } ?>

Upload another file

[/code]

magic_quotes_runtime and magic_quotes_gpc are both disabled.

The Table structure looks like this:

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 | | +---------+-------------+-------------------+------+-----+---------+----------------+----------------------+---------+

I’m pretty sure my code is working. All I can think is that I need to change something with my MYSQL configuration on the Dreamhost server --which is why I’m asking this here. Any ideas?

Thanks in advance,

-Josh


#2

your collation should be binary, shouldn’t it?


#3

0x0a is a newline. Is it possible you’ve got a stray blank line in one of your PHP files before the <? tag?