开发者

Unable to download files- PHP

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("se开发者_运维知识库lect * 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');


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜