Q . How to force download example.php file instead of execute it on server?

Dev Singh. asked, Nov 13 ' 2016


I have example.php file that I want my website visitors to be able to download.

How can I set the link to example.php so it downloads as a file they can "save" instead of executing on the server?

(I hope that makes sense.)

I've though about renaming the file to "example.txt", but that could be complicated, and it still doesn't allow them to save without first selecting/pasting, etc.


  • PHP
  • files
  • download

1 Answers

Try this php function !!!!


function forceDownLoad($filename) {

    if (!file_exists($filename))
        die('File does not exist.');
    if (!is_readable($filename))
        die('File is not readable.');

    header("Pragma: public");
    header("Expires: 0"); // set expiration time
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Disposition: attachment; filename=" . basename($filename) . ";");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize($filename));



#or with full path.


Leave a Ansewer