views:

74

answers:

3

This is the code I am using. As suggested I have added the headers for content type and disposition.

<?php
header('Content-Disposition: attachment');
header('Content-Type: application/octet-stream');

$con = mysql_connect("localhost","root","admin");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("acl_cake", $con);

$result = mysql_query("select * from attachments");

while($row = mysql_fetch_array($result))
{
echo '<a href="'.$row[2].'" target="_blank">Download</a>--'.$row[3].'<br>';
}

mysql_close($con);
?> 

Prior to addition of headers, I would have a few links available on the webpage. Whenever I would click one of them, a new page is opened and content in that file is displayed in the new page.

Now after adding the headers, whenever I load a page, I get a popup which asks me to download my form rather than the file.

If I have the content-disposition:attachment; filename='file.txt', then on page load there is a pop up to download the file "file.txt", and none of the links are displayed onto the webpage.

I am not sure if I have made a mistake with something.

Thank you!

I am not sure if I have made a mistake with the headers.

+2  A: 

you need to send the content-disposition header to force a download

header("Content-Disposition: attachment; filename=\"my.file\"");

also set the content type explicitly:

header('Content-Type: application/octet-stream');
Raoul Duke
No offense, but what's the point in escaping double quotes when you can use single ones?
fabrik
Hm, I just copied this code from a google search because I couldn't remember exactly so there's no point really :)
Raoul Duke
headers should be placed at the start of the file isn't it? how would I get the filename then?? Is it optional or essential?
macha
Allright then. I'm always uncertain about what's the correct method.
fabrik
@fabrik - double-quotes cause variables included in the string to expand; single quotes don't. So if filename is being pulled in from an input or some other piece of code, rather than hard-coding, double-quotes are the way to go. See http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing for clarification.
EmmyS
@sai headers must be sent before any other output or you will get a warning "headers already sent". So it would be best to put them at the top, yes. The filename is arbitrary. You can put whatever you want in there. Don't know if it's optional.
Raoul Duke
Ok there is a bit of confusion! I added the headers and rather the links of files being downloaded, I get my own form being downloaded everytime I refresh. And when I click the links, text is the output!
macha
@sai how about you edit the question, improve your wording and show some code? It's hard to tell what your problem is without seeing your code.
Raoul Duke
Hello Raoul, I have edited my question, could you now take a look at the question. Thank you!
macha
+1  A: 

Add header Content-Disposition: attachment. See http://apptools.com/phptools/force-download.php

GôTô
A: 

if i understand you right, you wan't to give your users a link to download a text-file. if so, you can do this by setting an application/octet-stream-header for this file (e.g. using php).

note: in most cases, i think you shouldn't do this. it't the users choice how to deal with files that can be opened by the browser, and if the user wan't to download such files, hea can easily configure his browser to to this or that.

oezi
@oezi! I added this header "header('Content-Type: application/octet-stream');" at the top. But whenever I load the page, I get a pop up which lets me download the page. I am not sure what I am making a mistake with.
macha