| | 981 | |
|---|
| | 982 | /** |
|---|
| | 983 | * downloads a file to the browser (i.e. sends the file to the browser) |
|---|
| | 984 | * |
|---|
| | 985 | * @author Moshe Brevda mbrevda => gmail ~ com |
|---|
| | 986 | * @pram string |
|---|
| | 987 | * @pram string, optional |
|---|
| | 988 | * @pram string, optional |
|---|
| | 989 | */ |
|---|
| | 990 | function download_file($file, $name = '', $type = '') { |
|---|
| | 991 | if (file_exists($file)) { |
|---|
| | 992 | $name = $name ? $name : basename($file); |
|---|
| | 993 | $type = $type ? $type : 'application/octet-stream'; |
|---|
| | 994 | header('Content-Description: File Transfer'); |
|---|
| | 995 | header('Content-Type: ' . $type); |
|---|
| | 996 | header('Content-Disposition: attachment; filename=' . $name); |
|---|
| | 997 | header('Content-Transfer-Encoding: binary'); |
|---|
| | 998 | header('Expires: 0'); |
|---|
| | 999 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
|---|
| | 1000 | header('Pragma: public'); |
|---|
| | 1001 | header('Content-Length: ' . filesize($file)); |
|---|
| | 1002 | ob_clean(); |
|---|
| | 1003 | flush(); |
|---|
| | 1004 | readfile($file); |
|---|
| | 1005 | exit; |
|---|
| | 1006 | } else { |
|---|
| | 1007 | return false; |
|---|
| | 1008 | } |
|---|
| | 1009 | } |
|---|