0

我在名为 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 word?它现在会在网站上清除 Microsoft Word 文本,而不是在按钮上。请告知是否可以这样做。仅用于本地测试。非常感谢!

4

1 回答 1

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
}
于 2012-07-01T07:45:50.507 回答