我在我的本地服务器中存储了一个名为template.doc的示例文档。然后我将文件目录存储在我的sqlite表中。我设法调出文件目录的路径,但是如何允许用户下载呢?
代码
<form id="List" name="List" method="post" action="">
<select name="List" id="List">
<?php
if ($Choice !="No")
{
$path = $_SERVER['DOCUMENT_ROOT']."/WEB/";
$fullPath = $path.$_GET['$Choice'];
echo "<form id=\"form7\" name=\"form7\" method=\"post\" action=\"\">";
echo "<input type=\"submit\" name=\"Download\" id=\"Download\" value=".$fullPath."/>";
if (file_exists($fullPath))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullPath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fullPath));
ob_clean();
flush();
readfile($fullPath);
echo "<label>".$fullPath."</label>";
echo "<input type=\"submit\" name=\"Download\" id=\"Download\" value=".$fullPath."/>";
exit;
echo '</form>';
}
}
?>
如何在列表表单下创建一个按钮并下载微软的单词?它现在刷新网站上的Microsoft文字文本,而不是按钮。 请告知是否可以这样做。仅供本地测试。非常感谢!
答案 0 :(得分:2)
您可以使用readfile
并设置相应的标题以强制下载。
readfile归化页面的示例:
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
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;
}
?>
您的表单应该是这样的
<form name="download" action="download.php" method="post">
<input type="hidden" name="fileID" value="some file identifier" />
<input type="submit" name="submit" value="Download File" />
</form>
然后,在download.php
if(isset($_POST["submit"])) {
$file = $_POST['fileID'];
// download file
}