簡體   English   中英

如何創建URL以使文件可以使用HTML和PHP自動下載?

[英]How can I create an URL to make a file downloadable automatically using HTML and PHP?

我的問題:

我希望能夠向客戶發送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]

我怎樣才能解決這個問題?

dot.Py是否要根據您要與之交談的對象來更改products.xlsx文件的鏈接? IE瀏覽器。 一個客戶端可能具有特定的product.xlsx文件,其中包含一些信息,而另一個客戶端可能具有需要查看的完全不同的信息。

如果不是這種情況,則可以使用<a>標記來引用鏈接。

因此,例如:


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

然后,您所需要做的就是將其包含在頁面的<body> ,客戶端可以單擊鏈接下載文件。

如果要在加載網頁時使用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]替換為您的文件路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM