我正在尝试下载一个文件。我正在使用IE11。我已经尝试了几种方法来做到这一点。目前,我正在尝试使用具有Content-Disposition方法的header。根据人们给出的其他答案,我已经尝试了几种不同的方法。而且它确实可以下载。但它不是下载我指向的文件,而是下载它所写入的文件。因此,如果我告诉它在我的test.php文件中下载example.txt。它将只下载test.php。
以下是我尝试过的方法:
这是用test.html编写的:
<?php
$filename = "example.txt"
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
?>我也试过把它做成一个按钮:
<a href="download.php?file=example.txt">BUTTON</a>其中,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");我尝试过更多的细微变化和混合匹配。所有这些都存在.html或.php文件而不是example.txt文件下载的相同问题。有人知道为什么会发生这种情况吗?IE11中是否有不支持的内容?我不认为这是一个简单的语法错误,因为我从网上的其他答案中复制了大部分。我已经尝试在这个文件夹和其他文件夹中使用example.txt existing和not existing。
编辑:所以事实证明这些都是有效的,我只是用错了。我一直试图创建独立的文件来运行这段代码,这样我就可以在不干扰网站上其他函数的情况下对其进行测试,但这使得php文件没有实际正常运行所需的资源。当我把代码放到网站上的实际文件中时,它工作得很好。smh
发布于 2017-03-01 23:18:44
试试这个:
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;https://stackoverflow.com/questions/42535427
复制相似问题