如何创建URL以使文件可以使用HTML和PHP自动下载?

时间:2019-04-23 15:47:16

标签: php html .htaccess redirect download

我的问题:

我希望能够向客户发送mywebsite.com/products之类的链接,以便他们可以下载.xlsx文件,并查看我在给atm播种的内容。

是否可以创建类似于“ www.mywebsite.com/file”的链接,所以当用户访问该URL时,浏览器将提示您下载.xlsx文件?

这是我的products.html文件:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Refresh" content="0; url=http://mywebsite.com/products.xlsx"/>
  </head>
</html>

我已经将products.xlsx上传到了服务器上。

我注意到,如果我使用扩展名.html访问网站,则会提示用户下载文件。如果我访问时没有扩展名,则会导致404错误。

也许是我的.htaccess配置导致了此问题?

也许是Options +MultiViewsHTML RewriteCond ..

Options All -Indexes

DirectoryIndex index.php index.htm index.html

RewriteEngine on
RewriteBase /

# ---- Make pages render without their extension in the url
Options +MultiViews


# Force HTTPS on the subdomains/subdirectories login or admin
#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^(login|admin)\. [NC]
#RewriteCond %{REQUEST_URI} ^(login|admin)\. [NC,OR]
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Force WWW if no subdomain is given
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Remove php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# Remove html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

我该如何解决?

2 个答案:

答案 0 :(得分:1)

dot.Py是否要根据与您交谈的对象来更改products.xlsx文件的链接?就是一个客户可能有一个特定的product.xlsx文件,其中包含一些信息,而另一个客户可能具有需要查看的完全不同的信息。

如果不是这种情况,则可以使用<a>标签来引用链接。

例如:


<a href='/excel/products.xlsx' target="_blank">Click to Download Excel Document</a>

然后您要做的就是将其包含在页面上的<body>中,客户可以单击链接下载文件。

答案 1 :(得分:1)

如果要在加载网页时使用PHP来提供xlsx文件:

<?php
$fileName = 'Products.xlsx';
$filePath = "[PATH]/products.xlsx";
header('Content-disposition: attachment; filename=' . $fileName);
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($filePath));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($filePath);

用文件路径替换[PATH]。