herghost Posted September 24, 2009 Share Posted September 24, 2009 Hi all, I am having some issues with downloading a file from the database, its stored as a blob. Here is my download code: <?php include('../include/dbconnect.php'); $id=$_GET['id']; $query = "SELECT name, type, size, content " . "FROM upload WHERE post_no = '$id'"; $result = mysql_query($query) or die('Error, query failed'); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-length: $size"); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); echo $content; ?> This is called on a different page using a html href="". The issue I am having is that if you click on the download link you get a file like filename, however if you right click and save link as you get filename.extension which is obviously how I want it! What do I have to do get it to download as filename.extension on left click? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/175379-file-download-from-database-blob-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2009 Share Posted September 24, 2009 It works for me in both IE8 and FF3.5 using some made up data. Does $name include the extension or not and what exactly is the type and extension as the problem could be specific to the content-type and extension. The only thing technically wrong with the posted code is that the filename='...' attribute needs to be enclosed in quotes. Quote Link to comment https://forums.phpfreaks.com/topic/175379-file-download-from-database-blob-help/#findComment-924199 Share on other sites More sharing options...
herghost Posted September 24, 2009 Author Share Posted September 24, 2009 Hi mate, It was with a .doc file, ill give it a go with something else and see what happens. When it saves to the database the name is just the username of the member that uploaded it, I was relying on the type field which is automatically posting the mime type. Quote Link to comment https://forums.phpfreaks.com/topic/175379-file-download-from-database-blob-help/#findComment-924205 Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2009 Share Posted September 24, 2009 A slight correction to the above, apparently the filename= attribute MUST use double-quotes. Using single-quotes actually created a target file with single-quotes as part of the name. I tested using an actual .doc file and $type = 'application/msword';. With both IE8 and FF3.5 and both left-clicking and right-clicking/save link as... resulted in the file being saved with the correct filename and extension. I can recall that older IE browsers wanted to use the name of the .php file that was in the link when you did not correctly specify the attachment; filename=\"$name\"" Something specific to what or how you are doing this is causing the symptom. I would examine the actual filename and type in the database to see what it actually is. Quote Link to comment https://forums.phpfreaks.com/topic/175379-file-download-from-database-blob-help/#findComment-924245 Share on other sites More sharing options...
PFMaBiSmAd Posted September 24, 2009 Share Posted September 24, 2009 the name is just the username of the member that uploaded itYou have got to be freaking kidding. How would you expect it to get downloaded with the proper extension in every possible browser and in every possible situation. Quote Link to comment https://forums.phpfreaks.com/topic/175379-file-download-from-database-blob-help/#findComment-924329 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.