Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/137653-force-download/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/137653-force-download/#findComment-719478
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/137653-force-download/#findComment-719499
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.