使用 html 和 php 创建下载按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17784414/
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
Create a download button using html and php
提问by Kartic
I want to create a download button using HTML and PHP. Below is my HTML code -
我想使用 HTML 和 PHP 创建一个下载按钮。下面是我的 HTML 代码 -
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="download.php" method="post">
<input type="submit" name="submit" value="Download File" />
</form>
</body>
</html>
Now I have created another file, called "download.php" and kept it in the same directory as my HTML file. Code of the PHP file is given below -
现在我创建了另一个名为“download.php”的文件,并将它保存在与我的 HTML 文件相同的目录中。PHP文件的代码如下-
<?php
header('Content-Type: application/download');
header('Content-Disposition: attachment; filename="Sample.mp3"');
header("Content-Length: " . filesize("Sample.mp3"));
$fp = fopen("Sample.mp3", "r");
fpassthru($fp);
fclose($fp);
?>
Sample.mp3 is kept in same folder as HTML file. I got these codes from internet.
Sample.mp3 与 HTML 文件保存在同一文件夹中。我从互联网上得到这些代码。
Now my problem is, when I click on download button, content of the PHP file get opened rather than downloading the file.
现在我的问题是,当我点击下载按钮时,PHP 文件的内容被打开而不是下载文件。
Can anyone please help me to fix this issue.
任何人都可以帮我解决这个问题。
回答by Quentin
Now my problem is, when I click on download button, content of the PHP file get opened rather than downloading the file.
现在我的问题是,当我点击下载按钮时,PHP 文件的内容被打开而不是下载文件。
You need to run the script on:
您需要在以下位置运行脚本:
- A webserver
- That supports PHP
- That is configured to treat that file as PHP
- 网络服务器
- 那个支持PHP
- 配置为将该文件视为 PHP
回答by zkanoca
You probably not run your .php file in a web server.
您可能没有在 Web 服务器中运行 .php 文件。
- Please install a personal web server to your computer like XAMPP or WAMP
- Then put your files into the www folder
- Open your browser
- Type localhost/your_HTML_file
- 请在您的计算机上安装个人网络服务器,例如 XAMPP 或 WAMP
- 然后把你的文件放到www文件夹
- 打开浏览器
- 输入 localhost/your_HTML_file
回答by David Jaw Hpan
using PHP is more coding need ! If you are using html5 and only do this for download purpose ,you can use <a>
link,that is easy and simple
使用 PHP 更需要编码!如果您使用 html5 并且仅出于下载目的执行此操作,则可以使用<a>
链接,这很简单
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<a href="Sample.mp3" download>Download </a>
</body>
</html>
回答by Walk
This worked for me :)
这对我有用:)
<a href="/path/to/downloadfile.csv">
<button class="btn">
<i class="fa fa-download"></i> Download
</button>
</a>
回答by Mohammad Mahdi Naderi
Try this in php file:
在 php 文件中试试这个:
$file = 'Sample.mp3'
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;