我的服务器上有一个 CSV 文件。如果用户单击链接,它应该下载,但它会在我的浏览器窗口中打开。
我的代码如下所示
<a href="files/csv/example/example.csv">
Click here to download an example of the "CSV" file
</a>
这是一个普通的网络服务器,我的所有开发工作都在其中进行。
我尝试过类似的东西:
<a href="files/csv/example/csv.php">
Click here to download an example of the "CSV" file
</a>
现在我的内容csv.php
file:
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
现在我的问题是它正在下载,但不是我的 CSV 文件。它创建一个新文件。
.htaccess 解决方案
要强制下载服务器上的所有 CSV 文件,请在 .htaccess 文件中添加:
AddType application/octet-stream csv
PHP解决方案
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
readfile("/path/to/yourfile.csv");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)