Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/175379-file-download-from-database-blob-help/
Share on other sites

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.

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.

 

 

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.

the name is just the username of the member that uploaded it
You 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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.