Sometimes, we want to force file download with PHP.
In this article, we’ll look at how to force file download with PHP.
How to force file download with PHP?
To force file download with PHP, we can call header
to set the response header to force file download.
For instance, we write
$file_url = 'http://www.myremoteserver.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
to call header
with "Content-disposition: attachment; filename=\"" . basename($file_url) . "\""
to make the file download.
Then we call readfile
to read thre $file_url
to open the file and download it.
Conclusion
To force file download with PHP, we can call header
to set the response header to force file download.