Problem downloading BLOB image
Posted: Fri Mar 20, 2009 6:27 pm
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:
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
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"
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....
I have been able to uplaod and downlaod a text file correctly and read it, but not a .doc file or an image..... bizarre