Identifying a binary file's MIME type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blades
Forum Newbie
Posts: 7
Joined: Thu Dec 30, 2004 10:23 pm

Identifying a binary file's MIME type

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Have you looked at Mime Magic yet?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:sigh:

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

:roll:
blades
Forum Newbie
Posts: 7
Joined: Thu Dec 30, 2004 10:23 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
blades
Forum Newbie
Posts: 7
Joined: Thu Dec 30, 2004 10:23 pm

Post by blades »

I posted my reply to the wrong thread? Jeez. Thanks for putting it here, feyd.

blades
Post Reply