xQuasar Posted December 19, 2008 Share Posted December 19, 2008 I was wondering how you could force a file to down in a while statement in php? Ok maybe that's not the greatest explanation; maybe you guys can get it from looking at the following code: <?php if ($handle = opendir("$username_saved/extras")) { echo "<table cellspacing=10>"; while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo " <tr> <td width=50%>$file</td> <td width=25%><a href=$username_saved/extras/$file> DOWNLOAD</a> </td> <td width=16%></td> <td width=25%>DELETE</td> </tr> "; } } closedir($handle); } ?> How do I force them to DOWNLOAD the file no matter what file extension when they click download, instead of opening it in the browser? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/137653-force-download/ Share on other sites More sharing options...
Zane Posted December 19, 2008 Share Posted December 19, 2008 the files have to be in a virtual server where any MIME types are exempt from it....i believe. I could be wrong though, but that seems to me what the solution is. Otherwise if you try to have someone download a PHP file or an HTML file then they will be parsed by the server rather than sent as a file. your best solution: Google Virtual servers, and MIME type exemptions one example http://docs.sun.com/app/docs/doc/820-5704/6nh35mdbc?a=view Quote Link to comment https://forums.phpfreaks.com/topic/137653-force-download/#findComment-719478 Share on other sites More sharing options...
Kieran Menor Posted December 19, 2008 Share Posted December 19, 2008 You could read the files through a PHP script and have the script define the MIME-type as "application/octet-stream". That should in all cases trigger a download. $filename = $_GET['file']; header("Content-Type: application/octet-stream"); header("Content-Length: ".filesize($filename)); header("Content-Disposition: attachment; filename=\"$filename\""); readfile($filename); Note that you would have to introduce some security measures to make sure that people can't download files you don't wan't them to download. If you have all the files in the same folder, it would be wise to simply not allow slashes ( / ) in the supplied filename. I don't think that people would be able to download the files in multiple bits (like download managers do) using this method unless you put in support for HTTP ranges. More info here: http://www.php.net/manual/en/function.readfile.php#86244 Quote Link to comment https://forums.phpfreaks.com/topic/137653-force-download/#findComment-719499 Share on other sites More sharing options...
Adam Posted December 19, 2008 Share Posted December 19, 2008 http://www.phpfreaks.com/forums/index.php/topic,95433.0.html A Quote Link to comment https://forums.phpfreaks.com/topic/137653-force-download/#findComment-719503 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.