PHP Force Download

We used to just link to a file to download it, for example to let users download myfile.zip, you just link to it: domain.com/myfile.zip ...
But the problem is, what if you want to let user download a html file or maybe php file without executing it ? I mean if you link to file.html it will just load it, and not download and save it, so how to do that ?

the answer is very easy, for example mypage.html, you want to let visitors download it, so do the following:

The php code:
<?php

$file = 'file.php'; // Her choose any file !
if (file_exists(
$file))
{
header('Content-disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/force-download');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '. filesize(
$file));
header('Pragma: no-cache');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
readfile(
$file);
}
else
{
$errorfile = 'the file "' . $file ;
}


?>