【问题标题】:Downloading a file from a PHP server via a website [closed]通过网站从 PHP 服务器下载文件 [关闭]
【发布时间】:2014-12-27 16:05:09
【问题描述】:
我希望用户在我的网站上下载物理文件。文件位于:
C:/xampp/htdocs/myfile/uploads/*
我需要一个可以在点击时动态下载文件的 PHP 脚本。假设我有以下按钮,点击它会触发magic.php 脚本。
<a href="magic.php?file=name"> Download file </a>
我在magic.php中需要什么PHP代码来下载我在查询参数中传递的文件名?
【问题讨论】:
标签:
php
html
file-upload
download
【解决方案1】:
下载链接
<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;
}
?>
【解决方案2】:
也许你可以这样做:(如果我错了,请纠正我)
<a href="uploads/<?php echo $row['name']; ?>" download="download">