Php 内容处置下载错误的文件

Php 内容处置下载错误的文件,php,html,internet-explorer-11,content-disposition,Php,Html,Internet Explorer 11,Content Disposition,我正试图下载一个文件。我正在使用IE11。我试过几种方法来做到这一点。目前,我正在尝试使用带有内容处置方法的标题。根据人们给出的其他答案,我尝试了几种不同的方法。它确实可以下载。但它不是下载我指向它的文件,而是下载它所写的文件。如果我告诉它在test.php文件中下载example.txt。它将只下载test.php 以下是我尝试过的方法: 这是用test.html编写的: <?php $filename = "example.txt" header('Content-Type: text

我正试图下载一个文件。我正在使用IE11。我试过几种方法来做到这一点。目前,我正在尝试使用带有内容处置方法的标题。根据人们给出的其他答案,我尝试了几种不同的方法。它确实可以下载。但它不是下载我指向它的文件,而是下载它所写的文件。如果我告诉它在test.php文件中下载example.txt。它将只下载test.php

以下是我尝试过的方法:

这是用test.html编写的:

<?php
$filename = "example.txt"
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
?>
我尝试过更多的细微变化和混合搭配。它们都有相同的问题,.html或.php文件下载,而不是example.txt。有人知道为什么会这样吗?IE11中是否有不支持的内容?我不认为这是一个语法错误,因为我从网上的其他答案中复制了大部分。我在这个文件夹和其他文件夹中尝试了example.txt existing和not existing

编辑:原来这些都有用,我只是用错了。我一直在尝试创建独立的文件来运行此代码,以便在不受网站上其他函数干扰的情况下对其进行测试,但这使得php文件没有实际运行所需的资源。当我把代码放到网站上的实际文件中时,它工作得非常好。smh

试试这个:

 header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Length: ' . filesize($File));
 header('Content-Disposition: attachment; filename=' . basename($File));
 readfile($File);
 exit;

不要将
内容类型设置为它不需要的内容,因为这很接近。它似乎在看example.txt。但现在它在一个新选项卡中打开example.txt的内容,而不是给出下载提示;按标题(“内容类型:应用程序/八位字节流”);实际上它现在什么都没做。我意识到它只是在另一个选项卡中打开example.txt,因为我已经创建了指向它的链接。我试着把建议的代码放在test.html和download.php中。如果你看到我的编辑,你会发现我的问题在于文件的位置。你的答案确实有用,所以我要数一数。谢谢你的帮助。:)
<?php
$file = $_GET['file'];  
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename="'.$file.'"');
?>
<?php
header('Content-Type: application/download');
header('Content-Disposition: attachment; filename="example.txt"');
header("Content-Length: " . filesize("example.txt"));

$fp = fopen("example.txt", "r");
fpassthru($fp);
fclose($fp);
?>
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($File));
header("Connection: close");
 header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Length: ' . filesize($File));
 header('Content-Disposition: attachment; filename=' . basename($File));
 readfile($File);
 exit;