Problem downloading BLOB image

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
muay_tb
Forum Newbie
Posts: 1
Joined: Fri Mar 20, 2009 6:07 pm

Problem downloading BLOB image

Post by muay_tb »

Hi all,

apologies if this has been asked elsewhere but...

i have created a personal learning website where i allow the user to upload a file (in this example an image - XXXX.jpg) to a table in my MySQL database. I can successfully upload and store the image in the BLOB data field, however, here is the mad bit, when i click on a link to download the uploaded file i can download it but when i open it using windows preview all i get is the message, "File is not a supported file format" :? even though the name and content type of the file fetched are correct.

Here is the code:

Code: Select all

 
upload.php:
 
if (isset($_FILES['uploadedfile']) && $_FILES['uploadedfile']['size'] > 0)
        {
            $att_tmp_name = $_FILES['uploadedfile']['tmp_name'];
            $att_name = $_FILES['uploadedfile']['name'];
            $att_size = $_FILES['uploadedfile']['size'];
            $att_type = $_FILES['uploadedfile']['type'];
 
            $att_ext = substr($att_name, strrpos($att_name, '.') + 1);
                
            // open, read and close file:
            $fp = @fopen( $att_tmp_name, "r");
            $file_content = @fread($fp, filesize($att_tmp_name));
            $file_content = addslashes($file_content);
            @fclose($fp);
 
            if (!get_magic_quotes_gpc())
            {
                $att_name = addslashes($att_name);
            }
                        
                        // this is an object i have created (similar to a Java bean):    
            $att_obj = new Attachment();
            $att_obj->setName($att_name);
            $att_obj->setSize($att_size);
            $att_obj->setContentType($att_type);
            $att_obj->setObj($file_content); //contains content
            $att_obj->setExtension($att_ext);
    }
 
the code which adds it to the db:
 
$sql = "INSERT INTO attachments(attachments_name,attachments_type, ";
$sql .= "attachments_size,";
$sql .= "attachments_obj,attachments_ext,attachments_date)";
$sql .= " VALUES ('{$attachment->getName()}','{$attachment->getContentType()}',{$attachment->getSize()},";
$sql .= "'{$attachment->getObj()}', '{$attachment->getExtension()}',now())";
 
[b]the above works fine[/b]
 
now the download.php:
 
after fetching the database row and populating an $att object:
 
$data = stripslashes($att->getObj());
$name = $att->getName();
$size = $att->getSize();
$type = $att->getContentType();
    
header("Content-type: ".$type); 
header("Content-length: ". $size);
header("Content-Disposition: attachment; filename=" .$name);
print $data;
 
This gives me the download image in IE which is fine, filename is fine, size is fine...[b]but when i view the image...i get nothing[/b] apart from a message in windows saying this image is not of a supported format!!!  8O  :cry: 
 
How can that be when it classes the image as a .jpg!!! ...confused....
 
Anyone who has had similar problems or can bail me out please???

I have been able to uplaod and downlaod a text file correctly and read it, but not a .doc file or an image..... bizarre :crazy:
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: Problem downloading BLOB image

Post by NikBruce »

I've got the same problem now.

I've tried downloading swf, ai, pdf, jpg, png and mp3 files. Only the mp3 files works.

This is quite an old post, does no one have a solution?
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: Problem downloading BLOB image

Post by NikBruce »

by the way, this is the script that i'm using,

Code: Select all

<?php
 
if(!is_numeric($id)) { die("Invalid id specified"); }
 
$sql = "SELECT `mime`, `data`, `size`, `name` FROM `$proName` WHERE `id` = '$id'";
 
$result = mysql_query($sql) or die(mysql_error());
 
$count = mysql_num_rows($result);
 
if($count == 1)
{
 
while($row = mysql_fetch_array($result))
{
$mime = $row["mime"];
$data = $row["data"];
$size = $row["size"];
$name = $row["name"];
 
header("Content-type: $mime");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
header("Accept-Ranges: bytes");
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-transfer-encoding: binary");
 
echo $data;
}//from the while loop
 
}//from if($count == 1)
 
else
 
{
 
echo "Record doesn't exist.";
 
}
 
?>
 
NikBruce
Forum Newbie
Posts: 22
Joined: Wed Dec 23, 2009 5:16 am

Re: Problem downloading BLOB image

Post by NikBruce »

and I have this problem no matter what browser I use.
Post Reply