Question

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

Was it helpful?

Solution

resolved its a little mistake

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

OTHER TIPS

well is important to ensure that the propertie filename="NAME.xxx" must have doble coutes. inside de header.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top