1 回答
TA贡献1818条经验 获得超11个赞
不要发送多个Content-type标头,发送文件扩展名的适当类型。
if (isset($_GET['name'])){
$name = $_GET['name'];
$stmt = $conn->prepare("SELECT link_to_policy FROM policies WHERE name = ? LIMIT 1");
$stmt->bind_param('s', $name);
$stmt->execute();
$result = $stmt->get_result();
$result2 = $result->fetch_assoc();
$policytodownload = $result2["link_to_policy"];
if (file_exists($policytodownload)) {
$type_map = ['xls' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'doc' => 'application/msword',
'pdf' => 'application/pdf'];
$ext = pathinfo($policytodownload, PATHINFO_EXTENSION);
if (isset($type_map[$ext])) {
header("Content-Type: {$type_map[$ext]}");
}
header("Cache-Control: no-cache");
header('Content-Length: ' . filesize($policytodownload));
ob_clean();
flush();
readfile($policytodownload);
exit;
}
}
- 1 回答
- 0 关注
- 246 浏览
添加回答
举报