PHP - 点击按钮请求后台下载文件

奥利给!

前端

<a href="http://test.domin.com/download.php?act=getTemplate" target="_blank" download="导入模板">

PHP 后端

$out_filename = "导入模板"; // 文件名称
$tmpFilePath = APP_ROOT . PROJECT_PATH ."/static/clue/$out_filename.xls";
$fileName = $out_filename . '.xls';

$suffix = '.xls';
$ua = $_SERVER["HTTP_USER_AGENT"];

ob_end_clean();

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
if ($suffix == '.xlsx') {
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} else {
    header('Content-Type: application/vnd.ms-excel');
}
if (preg_match("/MSIE/", $ua) || preg_match('/edge/', $ua) || preg_match('/edg/', $ua)) {
    $encoded_filename = urlencode($fileName);
    $encoded_filename = str_replace("+", "%20", $encoded_filename);
    header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
    header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '"');
} else if (preg_match("/Safari/", $ua) || preg_match("/WeChat/", $ua)) {
    $attachment = 'attachment; filename*=utf-8\'\'' . rawurlencode($fileName);
    header('Content-Disposition: ' . $attachment);
} else {
    header('Content-Disposition: attachment; filename="' . $fileName .  '"');
}
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
echo file_get_contents($tmpFilePath);
die;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后端木木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值