Can not download files - PHP

advertisements

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.

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


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