PHP Force File Download

Code Snippets Force File Download
Share:

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();

Resulting Output:

Share:

Leave a Reply

Your email address will not be published.

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger