[英]How to download a file in PHP when clicking submit input button
我有一个带有输入提交按钮的表单,并且需要它,以便单击该按钮时可以下载文件。
我的if语句基本上只是检查文件是否存在,如果存在则开始下载文件。 该部分有效,但是我无法使其正常工作,因此仅在单击按钮时才下载文件。
目前,单击该按钮没有任何反应。
也许我没有将if语句放在正确的位置?
<form action='?module=clientsupport&call=landing-proco' method='post' enctype="multipart/form-data">
<input type='submit' name='download' class="button yellow" value="Download"/>
<?php
if (file_exists("D:/wwwroot/Workspace/George/Goliath/modules/test/file.txt")) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename("D:/wwwroot/Workspace/George/Goliath/modules/test/file.txt") . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize("D:/wwwroot/Workspace/George/Goliath/modules/test/file.txt"));
readfile("D:/wwwroot/Workspace/George/Goliath/modules/test/file.txt");
sleep(2);
unlink("D:/wwwroot/Workspace/George/Goliath/modules/test/file.txt");
exit;
}
?>
</form>
从html删除php代码,使用您的PHP代码创建新文件,并将表单的操作链接到该文件。 会很好的
看你的程序。
第1行:
<form action='?module=clientsupport&call=landing-proco' method='post' enctype="multipart/form-data">
…开始输出HTML文档。
然后您将获得:
header('Content-Description: File Transfer');
在这里您开始输出不是HTML文档的内容,但是为时已晚,因为您已经在HTML文档的中间 。
设计您的逻辑以输出一件事或另一件事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.