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 關注
- 273 瀏覽
添加回答
舉報