rondog Posted September 30, 2007 Share Posted September 30, 2007 I have a phpfile that is reading different kinds of files that I want to force download. My switch case statements are working and it is downloading them, however, its spitting out all this binary code. Any idea why that is so? I am just trying to force the download. thanks. <?php session_start(); if ($_SESSION['approved'] != 'yes') { header("Location: dlerror.php"); } else { $filename = $_POST['fname']; $filename = realpath("8d46y2g1/".$filename); echo($filename); $file_extension = strtolower(substr(strrchr($filename,"."),1)); switch ($file_extension) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; default: $ctype="application/force-download"; } if (!file_exists($filename)) { die("NO FILE HERE"); } echo($ctype); header("Content-Type: $ctype"); header("Content-Disposition: attachment; filename=\"".basename($filename)."\";"); set_time_limit(0); @readfile("$filename") or die("File not found."); } ?> Link to comment https://forums.phpfreaks.com/topic/71274-solved-headers-and-readfile/ Share on other sites More sharing options...
Orio Posted September 30, 2007 Share Posted September 30, 2007 When I made some force-download scripts, I assisted with the script in this tutorial. Make sure you have everything that it has there. I think if you will change your script this way, it will work: <?php session_start(); if ($_SESSION['approved'] != 'yes') { header("Location: dlerror.php"); } else { $filename = $_POST['fname']; $filename = realpath("8d46y2g1/".$filename); $file_extension = strtolower(substr(strrchr($filename,"."),1)); switch ($file_extension) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; default: $ctype="application/force-download"; } if (!file_exists($filename)) { die("NO FILE HERE"); } // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Type: $ctype"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize(basename($filename))); header("Content-Disposition: attachment; filename=\"".basename($filename)."\";"); set_time_limit(0); @readfile("$filename") or die("File not found."); } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/71274-solved-headers-and-readfile/#findComment-358579 Share on other sites More sharing options...
rondog Posted September 30, 2007 Author Share Posted September 30, 2007 awesome that seems to be working..thanks a lot man! Link to comment https://forums.phpfreaks.com/topic/71274-solved-headers-and-readfile/#findComment-358716 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.