Page 1 of 1

Identifying a binary file's MIME type

Posted: Tue Jan 24, 2006 4:31 pm
by blades
I'm trying to set up a script that will allow site visitors to download a Windows-based application installer, which is a binary file. The problem I'm having is that Internet Explorer (Win-6.0 SP2) does not execute my PHP to create the typical download panel.

My PHP script:

Code: Select all

<?php
if (isset($_POST['submit'])) {
$Clicked = ($_REQUEST['clicked']);
$filename = "MyProgramInstaller.exe";
$filetype = "octet-stream";
$Recip = "hello@world.net";
$Subj = "MyProgramInstaller download occurred";
$Msg = "A visitor has downloaded a copy of MyProgramInstaller.exe.";
$Sender = "downloads@website.com";
$Nickname = "Downloads";
	if ((isset($Clicked)) && (file_exists($filename))) {
			// Send the file.
			header ("Content-Type: application/$filetype");
			header ("Content-disposition: attachment; filename=$filename");
			readfile ($filename);
			// Send the email.
			mail("$Recip", "$Subj", "$Msg", "From: $Nickname <$Sender>\r\nReply-To: $Nickname <$Sender>\r\n" );
	}
}
?>
Here's the HTML form:

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
				<input type="hidden" name="clicked" value="TRUE">
				<input src="images/nav/download.gif" type="image" name="submit" value="Download Now!" alt="Download MyProgramInstaller" class="swapformbtn">
</form>
Regarding the MIME type (var $filetype), I've tried "octet-stream", "x-msdownload", "x-compressed" and "x-zip-compressed". Those last 2 I tried when noting that in Windows "MyProgramInstaller.exe" appears with the typical ZIP icon (folder & vise clamp) and Properties/Attribute setting of "Archive."

None have worked yet in Internet Explorer, but all work in Firefox.

I should add that the web site is on a shared commercial hosting server. I will not be able to edit any Apache config files, but must settle on using a MIME type in my script that a default installation of Internet Explorer knows how to handle.

Any help is most appreciated.

Posted: Tue Jan 24, 2006 7:46 pm
by John Cartwright
Have you looked at Mime Magic yet?

Posted: Tue Jan 24, 2006 9:16 pm
by feyd
:sigh:

Useful Posts wrote:Some helpful information determining a file's actual type: Upload Script

:roll:

Posted: Tue Jan 24, 2006 10:32 pm
by blades
Jcart wrote:Have you looked at Mime Magic yet?
Thanks, I have. The script I am using originally made use of the mime_content_type() function which also requires creating the magic.mime file (associates filename extensions with MIME types) and enabling it in php.ini.

Again though, on a shared hosting server, I do not have access to our PHP directories.

Posted: Wed Jan 25, 2006 12:42 am
by feyd
blades (mistakenly?) chose to post this in the thread I linked:
blades wrote:Thanks to those who replied, but I found the solution.

As it turned out, it had nothing to do with identfying the MIME type, but was about using an image as a form button.

Apparently, Internet Explorer does not return a "submit" value when <input type="image">. It DOES return the mousedown coordinates (x & y) for the button click action, though, as do other browsers.

One simple change to my script...

Code: Select all

if (isset($_POST['submit_x']))
...fixed the problem in IE.


And as to this, I would normally say, do not look in the submission data for the submit button. It is not guaranteed (nor is any other field really) to exist in the submitted data. Instead, if you must, look for a field that would always be submitted, such as a hidden one, or a simple text (enabled) field. You can also use $_SERVER['REQUEST_METHOD'] to check if they are calling the page using POST.

Posted: Wed Jan 25, 2006 1:21 am
by blades
I posted my reply to the wrong thread? Jeez. Thanks for putting it here, feyd.

blades