phpgoogle-chromeheaderdownloadforce-download

Chrome Double headers sent by the server


i use php to force the download of any file and this is my code


index.php

<a href="download.php?file_name=example.png"><button class="details_button_T">Télécharger</button></a>

download.php

$file= $_GET['file_name'];
if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit();
    }

the code works in all browsers only chrome displays an error : Double headers sent by the server


Solution

  • resolved its a little mistake

    header('Content-Disposition: attachment; filename="'.basename($file).'"');