Reputation: 31
I have spent a couple of day's looking for a solution to start a download using php, of course I did find loads of solutions. Unfortunately none of them worked for me. For whatever reason when I test it and try to start the download, it does show the file I want but when I download it regardless of what kind of file it was meant to download it would download as a text file with my website's HTML within it.
Here is an example of one of the many examples I have tried:
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($File));
header("Connection: close");
Upvotes: 2
Views: 99
Reputation: 23316
Rather than trying to serve files with PHP, hand it off to the server with x-sendfile
headers. Performance is a lot better (especially for large files) and the server will handle the necessary response headers.
There is an Apache mod and it's built into Lighttpd and Nginx (with a slightly different name).
Upvotes: 1
Reputation: 9918
Can you try this please? You need readfile()
function.
<?php
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;
?>
Upvotes: 3