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:
// 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-Disposition: attachment;filename=\"$name\"");
header("Content-Description: PHP Generated Data");
header("Content-transfer-encoding: binary");
echo $size;
echo $type;
$content = stripslashes ($content);
echo $content;
5 Years
Discussion Span
Last Post by sasankasekhar

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

Edited by arcticM


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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.