[英]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.