[英]Force download PDF file from URL in wordpress
在 WordPress 中,如何創建指向“file.pdf”等文件的鏈接並強制下載而不是在瀏覽器中打開文件?
只需在 a 標簽上添加下載屬性(下載)並將其留空作為文件的默認文件名。
示例: >WordPress Download PDF</a>
href="http://wordpress.org/download/download.pdf" target="_blank" download >WordPress Download PDF</a>
在主題文件夾中制作 download.php
<?php
$url = $_REQUEST['file_url'];
$filename = basename($url);
$filetype = filetype($url);
header('Content-Disposition: attachment; filename=' . $filename);
header("Content-type: " . $filetype); // act as image with right MIME type
ob_clean();
flush();
readfile($url);
exit;
然后從 html 頁面點擊錨鏈接。 在此代碼中,您的文件 url 可能是相同的域或不同的域並不重要
<a href="https:://domain.com/wp-content/themes/gallerywp/download.php?file_url=fileurl" target="_blank">click to download</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.