【问题标题】:.htaccess does not allow for automatic file download.htaccess 不允许自动下载文件
【发布时间】:2015-05-14 10:19:07
【问题描述】:

我的网站上有下载文件的链接:

<a href="documents/myfile.pdf">Download</a>

但是,当我单击链接时,浏览器不会自动下载此文件,而是将我重定向到文件 url http://example.com/documents/myfile.pdf 我可以在其中看到其内容。我只知道我的 .htaccess 文件有问题,但我不知道它可能是什么。我使用来自 html5boilerplate 项目的 .htaccess,您可以在此处预览源代码:https://raw.githubusercontent.com/h5bp/html5-boilerplate/master/dist/.htaccess。感谢您的帮助。

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    使用 htaccess 强制下载文件的最佳方法是在这里。我提供了一个完整的示例,将所有文件放在根目录中并对其进行测试:)

    .htaccess

    RewriteEngine on
    RewriteRule ^(.*).(txt|pdf|zip|rar)$ /download.php?file=$1.$2 [R,L]
    

    下载.php

    <?php
      if (!empty($_GET['file'])) {
        $file = basename($_GET['file']);
        $type = array("txt", "pdf", "zip", "rar");
        $exts = strtolower(substr(strrchr($file, "."), 1));
        if (!in_array($exts, $type)) {
          header("HTTP/1.0 403 Forbidden");
          exit('File not allowed!');
        } else {
          if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="' . $file . '"');
            header('Content-Transfer-Encoding: binary');
            header('Connection: Keep-Alive');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . sprintf("%u", filesize($file)));
            $fh = fopen($file, "rb");
            while (!feof($fh)) {
              echo fgets($fh);
              flush();
            }
            fclose($fh);
            exit;
          } else {
            header("HTTP/1.0 404 Not Found");
            exit('File not found!');
          }
        }
      }
    ?>
    

    link_page.php

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Download Page</title>
    <link rel="stylesheet" type="text/css" href="link_page.css">
    </head>
    <body>
    <div class="mainbox">
      <div class="box1">
        <span class="title">Download Page</span>
      </div>
      <div class="cleardiv"></div>
      <div class="sbar"></div>
      <div class="cleardiv"></div>
      <div class="box2">
        <span class="row">File: <a href="file.zip" class="link">file.zip</a> Click to download file.zip</span><br />
        <span class="row">File: <a href="file.rar" class="link">file.rar</a> Click to download file.rar</span><br />
        <span class="row">File: <a href="file.txt" class="link">file.txt</a> Click to download file.txt</span><br />
      </div>
      <div class="cleardiv"></div>
    </div>
    </body>
    </html>
    

    link_page.css

    @import url(http://fonts.googleapis.com/css?family=Oswald);
    html {
      display: table;
    }
    html, body {
      width: 100%;
      height: 100%;
    }
    body {
      color: black;
      display: table-cell;
      vertical-align: middle;
      background-color: lightgray;
      font-family: Oswald, 'Helvetica Neue', Helvetica, Arial, sans-serif;
      font-size: 14px;
      font-style: normal;
      line-height: normal;
      font-weight: normal;
      font-variant: normal;
    }
    .mainbox {
      border-radius: 7px;
      border: 1px solid gray;
      background-color: darkgray;
      width: 400px;
      min-height: 100px;
      padding: 10px;
      margin: 0 auto;
      margin-top: 50px;
      margin-bottom: 50px;
    }
    .box1 {
      vertical-align: middle;
      text-align: center;
      margin: 0 auto;
      padding: 10px;
    }
    .box2 {
      vertical-align: middle;
      text-align: center;
      margin: 0 auto;
      padding: 10px;
    }
    .title {
      font-family: Oswald, 'Helvetica Neue', Helvetica, Arial, sans-serif;
      font-size: 19px;
      font-style: normal;
      line-height: normal;
      font-weight: normal;
      font-variant: normal;
      vertical-align: middle;
      text-align: center;
      margin: 0 auto;
      padding: 10px;
    }
    .row {
      font-family: Oswald, 'Helvetica Neue', Helvetica, Arial, sans-serif;
      font-size: 14px;
      font-style: normal;
      line-height: normal;
      font-weight: normal;
      font-variant: normal;
      vertical-align: middle;
      text-align: left;
      float: right;
      width: 295px;
      margin: 0 auto;
      padding: 10px;
    }
    a.link:link {
      color: #E6E6FA;
      text-decoration: none;
      background-color: #006699;
      border: black 1px solid;
      border-radius: 5px;
      padding: 4px;
    }
    a.link:visited {
      color: #E6E6FA;
      text-decoration: none;
      background-color: #006699;
      border: black 1px solid;
      border-radius: 5px;
      padding: 4px;
    }
    a.link:hover {
      color: #E6E6FA;
      text-decoration: none;
      background-color: #003399;
      border: black 1px solid;
      border-radius: 5px;
      padding: 4px;
    }
    .cleardiv {
      clear: both;
    }
    .sbar {
      width: 90%;
      border-top: 1px solid gray;
      text-align: center;
      vertical-align: middle;
      margin: 0 auto;
    }
    

    download my example and test it

    【讨论】:

      【解决方案2】:

      你可以试试:

      <FilesMatch "\.(gz|pdf|zip|rar)$" >
          Order allow,deny
          Allow from all
          Satisfy any
      </FilesMatch>
      

      【讨论】:

      • 我在我的 htaccess 底部添加了这个,但不幸的是它不起作用
      • 谢谢,你已经接近正确答案了,我发布了可行的解决方案:)
      【解决方案3】:

      【讨论】:

      • 它没有得到广泛的支持,所以对我来说不是一个好的解决方案。
      【解决方案4】:

      解决方案:

      <FilesMatch "\.(txt|pdf|zip|rar)$">
      
        ForceType application/octet-stream
        Header add Content-Disposition "attachment"
      
        Order allow,deny
        Allow from all
        Satisfy any
      
      </FilesMatch>
      

      【讨论】:

        猜你喜欢
        • 2018-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-22
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 2015-08-31
        相关资源
        最近更新 更多