I have a project in which I desire to upload and download blob files in php. I have written the codes to upload the files into mysql however, when I try to download the files, be it pdf, .doc or jpeg, the error message I get is "The selected file can not be opened". I need assistance in this regard pls. The download codes are:

PHP Code:
<?php
if(isset($_GET['download']))
{
// if id is set then get the file with the id from database
include 'open_db.inc';
$name    = $_GET['download'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE name = '$name'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Cache-Control: maxage=1"); //In seconds
header("Pragma: public");
header("Content-length:$size");
header("Content-type:$type");
header("Content-type:application/pdf");
header("Content-Disposition: attachment;filename=\"$name\"");
header("Content-Description: PHP Generated Data");
header("Content-transfer-encoding: binary");
ob_end_clean();
echo $size;
echo $type;
$content = stripslashes ($content);
echo $content;
exit;
}
?>

Recommended Answers

All 5 Replies

Member Avatar for arcticM

not sure if it'll work for ur need, but when I needed option for files to be uploaded and downloaded, I simply saved the url of the file in my DB as text and then display it as a regular link. when someone clicks on the link the file would open in Word/adobe reader/etc (depending on the file type.. so simle!
`echo'<a href="'.$full_URL.'"> <img title="open" style="border-style: none" src="Images/icons/open.png" /></a>';

Thanks articM. However, I have so many files that would be save into the DB. Hence, I do not want a situation that I will using URL to locate the files. I will prefer that all my files are saved in a database and can be downloaded. It is just like downloading blobs but it is working well in my case. Thanks again.

There may be a problem with the headers.
You can try removing the two echos from line 23 and 24 and keep only one echo --

echo $content;

and execute.

Oh sasankasekhar, thanks so much. You have really done it for me. I am exceeding grateful. Now I can go to the next aspect of the project. I removed the two echo on line 23 and 24 but did not touch the headers. Bang it worked. Eureka. Thanks a million.

Hi omoayan, please mark the post as solved if it is solved.
Thanks, ... keep smiling.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.