通过网站从PHP服务器下载文件

通过网站从PHP服务器下载文件,php,html,file-upload,download,Php,Html,File Upload,Download,我有物理文件,我希望用户下载到我的网站上。这些文件位于: C:/xampp/htdocs/myfile/uploads/* 我需要一个PHP脚本,可以下载文件动态点击。假设我有一个按钮,单击它会触发magic.phpscript 在magic.PHP中我需要什么PHP代码来下载我在查询参数中传递的文件名?也许你可以这样做:(如果我错了,请纠正我) 下载链接 <a href="magic.php?file=<?php echo urlencode($row['name']); ?&g

我有物理文件,我希望用户下载到我的网站上。这些文件位于:

C:/xampp/htdocs/myfile/uploads/*

我需要一个PHP脚本,可以下载文件动态点击。假设我有一个按钮,单击它会触发
magic.php
script


magic.PHP
中我需要什么PHP代码来下载我在查询参数中传递的文件名?

也许你可以这样做:(如果我错了,请纠正我)

下载链接

<a href="magic.php?file=<?php echo urlencode($row['name']); ?>">Download</a>

magic.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;
}
?>

<?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;
}
?>