在名为 download.php 的分页中,包含以下代码:
<?php
$filename = 'file.pdf';//this should be the name of the file you want to download
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
?>
然后,您的主页应该有一个指向下载页面的链接,如下所示:
<a href="download.php">DOWNLOAD</a>
让我知道这是否适合您。
Edited:
我之前的例子是下载 pdf 文件。如果您想下载不同类型的文件,则必须稍微修改几行。我建议您首先尝试使用之前的代码下载 pdf 文件,然后在其他文件上完成测试。
要从数据库检索路径,您可以使用 MySQL (PDO)。
$sqlStatement = "SELECT path FROM my_table WHERE some_id = ".$something;
/*if you are retrieving the path from the database,
you probably have a lot of different paths available
there, so only you know the criteria which will decide
which of the many paths it is that you choose to extract*/
$sqlPrepared = $connection->prepare($sqlStatement);
$sqlPrepared->execute();
$row_info = fetch($sqlPrepared);
$filename = $row_info['path'];// this would be the $filename = 'file.pdf'
//that was in the example above
如果你不确定如何连接数据库,网上有很多解释MySQL的文章,相对简单。
我希望这有帮助:)