强制使用PHP下载文件 [英] Forcing to download a file using PHP
本文介绍了强制使用PHP下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的服务器上有一个 CSV
文件,如果用户点击应该下载的链接,而是在浏览器窗口打开。
I have a CSV
file on my server, if a user clicks on a link it should download, but instead it opens up in my browser window.
我的代码如下
<a href="files/csv/example/example.csv">
Click here to download an example of the "CSV" file
</a>
这是一个Web服务器正常的Web服务器,我的所有开发人员都在工作。
It's a web server normal webserver where I have all of my dev work on.
我尝试过像
<a href="files/csv/example/csv.php">
Click here to download an example of the "CSV" file
</a>
现在我的 csv.php
文件:
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
现在我的问题是下载,但不是我的 CSV
文件创建一个新文件。
Now my issue is it's downloading but not my CSV
file it's creating a new file.
推荐答案
.htaccess解决方案
要强制您的服务器上的所有csv下载,请添加您的.htaccess文件:
.htaccess Solution
To brute force all csv's on your server to download, add in your .htaccess file:
AddType application/octet-stream csv
PHP解决方案
PHP Solution
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
readfile("/path/to/yourfile.csv");
这篇关于强制使用PHP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文