将此代码放在页面上(与PHP代码一起从数据库获取信息并将其放在名称/大小/数据的变量中,然后链接到该页面.
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $name_of_file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size_of_file);
echo $file_data;
?>
并非所有上面列出的标头都是必需的 – 实际上,只有Content-Type标头才能使下载正常工作. Content-Disposition标头最好包含,以便您可以指定正确的文件名;其他人只是帮助浏览器更好地处理下载,如果你愿意,可以省略.