使用readfile()下載檔案

flowerszhong發表於2016-10-04

下載圖片

<?php
$file = `monkey.gif`;

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;
}
?>

下載xlsx文件

header(`Content-Description: File Transfer`);
    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    header("Content-Disposition: attachment; filename="".basename($fileLocation).""");
    header("Content-Transfer-Encoding: binary");
    header("Expires: 0");
    header("Pragma: public");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header(`Content-Length: ` . filesize($fileLocation)); //Remove

    ob_clean();
    flush();

    readfile($fileLocation);


相關文章