php 下载按钮,按钮下载.txt文件(PHP和HTML)

你为什么不把这个代码放在一个单独的文件中,比如download.php:

$file = "logs/{$session->username}.txt";

if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");

$type = filetype($file);

// Get a date and timestamp

$today = date("F j, Y, g:i a");

$time = time();

// Send file headers

header("Content-type: $type");

header("Content-Disposition: attachment;filename={$session->username}.txt");

header("Content-Transfer-Encoding: binary");

header('Pragma: no-cache');

header('Expires: 0');

// Send the file contents.

set_time_limit(0);

readfile($file);

?>

请注意,您必须将引号更改为双引号,因为您在内部使用变量.因此,要扩展变量,请将第一行更改为:

$file = "logs/{$session->username}.txt";

在这里,我考虑$session-> username,作为您尝试引用的变量.

并在HTML中有这个:

Download All Your Keys On A .txt

当您单击此按钮时,它会重定向到download.php,从而启动txt文件的下载.就如此容易.但这需要你有两个文件.我不明白这里需要一个按钮.为什么不使用像这样的简单链接?

Download All Your Keys On A .txt

如果需要,可以使用CSS设置样式.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值