1 回答
TA贡献1817条经验 获得超6个赞
只需将表单发送到 download.php 脚本。
<form method = "post" action="<url-to>download.php">
<select class="form-control" name="downloaditem">
<!-- It's not necessary to make the first option available for select -->
<option value="0" disabled>Select a file to download</option>
<option value="1">File one</option>
<option value="2">File two</option>
<option value="3">File three</option>
<option value="4">File four</option>
</select>
<button type="submit" class="btn btn-info" name="files">Download</button>
</form>
下载.php
// Get the file name and path to the file based on the given id
// or return null
function getFile($id) {
switch((int)$_POST['downloaditem']) {
case 1:
return ['file1', 'path/to/file1.txt'];
case 2:
return ['file1', 'path/to/file2.txt'];
case 3:
return ['file1', 'path/to/file3.txt'];
default:
return null;
}
}
if(isset($_POST['files'])) {
$file = getFile($_POST['downloaditem']);
if (null !== $file} {
header('Content-Disposition: attachment; filename=' . $file[0]. '.txt');
header('Content-Type: application/txt');
readfile($file[2]);
header('results.php');
} else {
// If function returns null show a HTTP 404 error
header('Content-Type: text/plain');
header('HTTP/1.1 404 Not Found');
}
}
- 1 回答
- 0 关注
- 117 浏览
添加回答
举报