About
In this code snippet, we’ll learn how to force file downloads in PHP.
Let’s see the example code below.
Code:
<?php $filePath = "C:\\xampp\\htdocs\\Code Examples\\php\\force file downloads\\fileToDownload.txt"; header("Content-Description: File Transfer"); header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"". basename($filePath) ."\""); readfile($filePath); exit();