.htaccess不允许自动下载文件

时间:2015-05-14 10:19:07

标签: apache .htaccess

我在我的网站链接上下载了一些文件:

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

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

4 个答案:

答案 0 :(得分:0)

您可以尝试:

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

答案 1 :(得分:0)

答案 2 :(得分:0)

<强>解决方案:

<FilesMatch "\.(txt|pdf|zip|rar)$">

  ForceType application/octet-stream
  Header add Content-Disposition "attachment"

  Order allow,deny
  Allow from all
  Satisfy any

</FilesMatch>

答案 3 :(得分:0)

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

的.htaccess

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

的download.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