我有物理文件,我希望用户在我的网站上下载。这些文件位于:
C:/xampp/htdocs/myfile/uploads/*
我需要一个可以在单击时动态下载文件的 PHP 脚本。假设我有以下按钮,单击该按钮时会触发脚本magic.php
。
<a href="magic.php?file=name"> Download file </a>
magic.php
,我需要什么 PHP 代码来下载我在查询参数中传递的文件名?
下载链接
<a href="magic.php?file=<?php echo urlencode($row['name']); ?>">Download</a>
魔术.php页面
<?php
$file = 'C:/xampp/htdocs/myfile/uploads/'.urldecode($_GET['file']);
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
也许你可以做这样的事情:(如果我错了,请纠正我)
<a href="uploads/<?php echo $row['name']; ?>" download="download">