如何允许用户从php服务器下载文件
问题描述:
我已经构建了一个工具,供我的用户上传他们的文档。如何允许用户从php服务器下载文件
但是,我发现很难让用户从我的网站上下载他们的文档。我可以在我的服务器上看到用户上传的文件,位于C:/xampp/htdocs/myfile/uploads/
。
我假设的最终结果可能是类似于:<a href="magic.php"> Download file </a>
答
下载链接
<a href="magic.php?file=<?php echo urlencode($row['name']); ?>">Download</a>
magic.php page
<?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">