HGeneAnthony Posted December 24, 2009 Share Posted December 24, 2009 I would like to have it that when a user clicks on a file it forces a file download instead of opening in the browser. Is this possible with php or do I need to use javascript? Quote Link to comment https://forums.phpfreaks.com/topic/186226-how-can-i-force-a-file-to-download-instead-of-opening/ Share on other sites More sharing options...
trq Posted December 24, 2009 Share Posted December 24, 2009 Take a look in the FAQ/Code snippet board, there is a script in there. Quote Link to comment https://forums.phpfreaks.com/topic/186226-how-can-i-force-a-file-to-download-instead-of-opening/#findComment-983505 Share on other sites More sharing options...
PravinS Posted December 24, 2009 Share Posted December 24, 2009 You can use this function function force_download($file) { $dir = ""; if ((isset($file))&&(file_exists($dir.$file))) { header("Content-type: application/force-download"); header('Content-Disposition: inline; filename="' . $dir.$file . '"'); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($dir.$file)); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $file . '"'); readfile("$dir$file"); } } Quote Link to comment https://forums.phpfreaks.com/topic/186226-how-can-i-force-a-file-to-download-instead-of-opening/#findComment-983507 Share on other sites More sharing options...
HGeneAnthony Posted December 24, 2009 Author Share Posted December 24, 2009 Thank you I'm going to try that now! Quote Link to comment https://forums.phpfreaks.com/topic/186226-how-can-i-force-a-file-to-download-instead-of-opening/#findComment-983520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.