下载 .txt 文件的按钮(PHP 和 HTML)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17310453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Button To Download .txt File (PHP & HTML)
提问by RepairServices
Ok so I want to my users to be able to download a .txt
file with their username on it.
好的,所以我希望我的用户能够下载.txt
带有他们用户名的文件。
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:
我正在尝试这样做,以便当您单击按钮时,它会强制下载配置为的 .txt 文件:
$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!
提前致谢!
回答by Praveen Kumar Purushothaman
Why don't you have this code in a separate file, say download.php
:
为什么不在单独的文件中包含此代码,例如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");
//** If you think header("Content-type: $type"); is giving you some problems,
//** try header('Content-Type: application/octet-stream');
//** Note filename= --- if using $_GET to get the $file, it needs to be "sanitized".
//** I used the basename function to handle that... so it looks more like:
//** header('Content-Disposition: attachment; filename=' . basename($_GET['mygetvar']));
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);
ob_clean();
flush();
readfile($file);
//** If you are going to try and force download a file by opening a new tab via javascipt
//** (In this code you would replace the onClick() event handler in the html
//** button with onclick="window.open('www.someurl.com', '_blank');"
//** - where 'www.someurl.com' is the url to the php page - I keep the file
//** creation and download handling in the same file, and $_GET the file name
// - bad practice? Probably, but I never claimed to be an expert),
//** be sure to include exit(); in this part of the php...
//** otherwise leave exit(); out of the code.
//** If you don't, it will likely break the code, based on my experience.
//exit();
?>
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:
请注意,您必须将引号更改为双引号,因为您在'
s 中使用了一个变量。因此,要扩展变量,请将第一行更改为:
$file = "logs/{$session->username}.txt";
Here I consider, $session->username
, as the variable you are trying to refer.
在这里,我认为 ,$session->username
作为您要引用的变量。
And have this in the HTML:
在 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?
当您单击此按钮时,它会重定向到download.php
,开始下载 txt 文件。就如此容易。但这需要您有两个文件。我不明白这里需要一个按钮。为什么不使用这样的简单链接?
<a href="download.php">Download All Your Keys On A .txt</a>
And if you need, you can style it using CSS.
如果需要,您可以使用 CSS 设置样式。