【问题标题】:Download the file instead of opening new window下载文件而不是打开新窗口
【发布时间】:2015-01-23 10:29:18
【问题描述】:

用户可以在我的表单上上传文档和详细信息,这些表单会保存到数据库中,并创建一个文件夹来保存文档。他们可以上传 .jpg .png .docx 等。所以不仅仅是图片。

我现在想显示这些文档的链接,当您单击链接时,它会提供在您的计算机上保存/打开文件的选项。相反,当我单击该链接时,它会转到一个新页面并在那里显示图像。但是因为他们可以上传word文档pdf等。你不能在新窗口中看到这些。

我现在使用的方法不支持IE,这是必不可少的。

代码:

  if(isset($_POST["submit"])){

        $ref = $_POST["reference"];
        $query  = "SELECT * ";
        $query .= "FROM customers ";
        $query .= "WHERE reference = {$ref}";
        $results = mysqli_query($connection, $query);

        // Array to hold all results:
        $all = array();
        while ($customer_details = mysqli_fetch_assoc($results)) {
            // Append a new sub-array of the 3 other cols to
            // the main array's key by reference using the []
            // array append syntax
            $all[$customer_details['reference']][] = array(
                'doc1' => $customer_details['doc1'],
                'doc2' => $customer_details['doc2'],
                'doc3' => $customer_details['doc3']
            );
        }

        foreach ($all as $reference => $rows) {
          // Write out the reference, which was the array key
            echo $reference . "<br/>";
            // Then in a loop, write out the others
            foreach ($all as $reference => $rows) {
          // Write out the reference, which was the array key
            echo $reference . "<br/>";
            // Then in a loop, write out the others
            foreach ($rows as $row) {
                echo  "<a href='docs/1111111/1/" . $row['doc1'] . "' download='" . $row['doc1'] . "' >file1</a><br/>";
                echo  "<a href='docs/1111111/1/" . $row['doc2'] . "' download='" . $row['doc2'] . "' >file2</a><br/>";
                echo  "<a href='docs/1111111/1/" . $row['doc3'] . "' download='" . $row['doc3'] . "' >file3</a><br/>";
            }

        }
    }

我该如何解决这个问题?

【问题讨论】:

    标签: php html download


    【解决方案1】:

    您只需将代码行添加到服务器上的 .htaccess 文件中。 您可以根据扩展名(例如 doc)更改文件类型

    <FilesMatch "\.(?i:pdf)$">
      ForceType application/octet-stream
      Header set Content-Disposition attachment
    </FilesMatch>
    

    这里有 3 个选项,这是第 3 个选项: http://www.tipsandtricks-hq.com/forum/topic/force-a-file-to-download-instead-of-showing-up-in-the-browser

    【讨论】:

      猜你喜欢
      • 2017-03-05
      • 2012-09-01
      • 2020-12-10
      • 2018-06-02
      • 1970-01-01
      • 2014-12-28
      • 2011-11-18
      • 2012-08-09
      • 2013-11-03
      相关资源
      最近更新 更多