按钮下载.txt文件(PHP和HTML) [英] Button To Download .txt File (PHP & HTML)

查看:143
本文介绍了按钮下载.txt文件(PHP和HTML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我希望我的用户能够下载带有用户名的 .txt 文件。



我到处检查过,到目前为止我找不到我想要的或者甚至可能的。



给你举个例子, m试着做,这里是我的代码:

 < button type =button>下载A上的所有密钥.txt 
<?php
$ file ='logs / $ session-> username.txt';

if(!file_exists($ file))die(对不起,该文件似乎不存在。);

$ type = filetype($ file); //获取日期和时间戳$ today = date(F j,Y,g​​:i a); $ time = time(); //发送文件头标题(Content-type:$ type); header(Content-Disposition:attachment; filename = $ filename);
头(Content-Transfer-Encoding:binary);
header('Pragma:no-cache');
header('Expires:0'); //发送文件内容。
set_time_limit(0);
readfile($ file);
?>
< / button>

我试图让它在点击按钮时强制下载.txt文件配置为:

  $ file ='logs / $ session-> username.txt'; 

对不起,如果这是令人困惑和混乱的,但没有其他方式可以呈现此内容。



预先感谢!

解决方案

在一个单独的文件中,比如 download.php

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

if(!file_exists($ file))die(对不起,该文件似乎不存在。);

$ type = filetype($ file);
//获取日期和时间戳
$ today = date(F j,Y,g​​:i a);
$ time = time();
//发送文件头
header(Content-type:$ type);
header(Content-Disposition:attachment; filename = {$ session-> username} .txt);
头(Content-Transfer-Encoding:binary);
header('Pragma:no-cache');
header('Expires:0');
//发送文件内容。
set_time_limit(0);
readfile($ file);
?>

请注意,您必须将引号更改为双引号,因为您在 秒。因此,要扩展变量,请将第一行更改为:

  $ file =logs / {$ session->用户名}。文本; 

这里我认为, $ session->用户名,作为你想要引用的变量。



在HTML中有这个:

<$ p $ <> c $ c>< button type =buttononclick =location.href ='download.php'>在A .txt下载所有您的密钥< / button>

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

 < a href =download.php>下载所有密钥在.txt上< / a> 

如果您需要,您可以使用CSS来设计它。


Ok so I want to my users to be able to download a .txt file with their username on it.

I've checked everywhere, and so far I can't find what I want or if it's even possible.

To give you an example of what I'm trying to do, here is my code:

<button type="button">Download All Your Keys On A .txt
<?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=$filename");
header("Content-Transfer-Encoding: binary"); 
header('Pragma: no-cache'); 
header('Expires: 0'); // Send the file contents.
set_time_limit(0); 
readfile($file);
?>
</button>

I'm trying to make it so that when you click the button, it force download's the .txt file configured with:

$file = 'logs/$session->username.txt';

Sorry if this is confusing and messy, but there's no other way for me to present this.

Thanks in advance!

解决方案

Why don't you have this code in a separate file, say download.php:

<?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);
?>

Please note that you have to change the quotes to double quotes, as you use a variable inside the 's. So, to expand the variable, change the first line to:

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

Here I consider, $session->username, as the variable you are trying to refer.

And have this in the HTML:

<button type="button" onclick="location.href='download.php'">Download All Your Keys On A .txt</button>

And when you click on this button, it redirects to the download.php, that initiates a download of the txt file. As simple as that. But this requires you to have two files. And I don't understand the need for a button here. Why not just use a simple link like this?

<a href="download.php">Download All Your Keys On A .txt</a>

And if you need, you can style it using CSS.

这篇关于按钮下载.txt文件(PHP和HTML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆