Pushing xpi mime content from php


A couple of days back I created a Firefox toolbar for my blog as an experiment in learning XUL. Once installed on my blog I wanted Firefox to recognize it as an addon and install it rather than displaying a ‘save/open’ dialog. For that I needed to add the following directive to the servers .htaccess file.

AddType application/x-xpinstall .xpi

But the problem was that my server (godaddy) for some mysterious reasons was not recognizing the above directive. Searching around I found that godaddy has some restrictions on the various htaccess directives (yuck!). After fiddling for some time I finally had to try another solution.

What the Apache ‘AddType’ directive does is that it maps the given filename extension onto the specified MIME content type, so that the browser can understand what it is supposed to do with the content. If we could replicate that in PHP than we are good. A possible solution that worked for me is shown below. You can also use the idea for other MIME types.

<?php
 
 
/* File we want to send to the browser */
$filename = "codediesel.xpi";
 
 
/**
 * The following header is required for browsers that do not
 * recognize the xpi extension. i.e all browsers other than Firefox.
 * This will display the familiar 'save/open' dialog if the xpi
 * extension is not supported.
 */
header("Content-Disposition: filename={$filename}");
 
 
/* Tell the browser that the content that is coming is an xpinstall */
header('Content-type: application/x-xpinstall');
 
 
/* Also send the content length */
header('Content-Length: ' . filesize($filename));
 
 
/* readfile reads the file content and echos it to the output */
readfile($filename);
 
?>

Of course if the filesize is large the readfile function could possibly incur a performance hit. But for small files it works good.

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

3 Responses

1

Daily News About Firefox : A few links about Firefox - Thursday, 14 May 2009 22:46

May 14th, 2009 at 11:04 pm

[...] Sending xpi mime content from php [...]

2

PaulG

May 20th, 2009 at 6:18 am

Yup, it works. Nice.

I wonder how I can improve the experience for my Admin users of my CMS with this though… kinda custom install with pre-selected options always on view.

Maybe this could provide direct links into to help?

Install could be controlled by only installing with the correct credentials, but how would I continue to poll for the user being valid.

Must give that some thought …

Cheers! Paul

3

Sameer Borate’s Blog:Pushing xpi mime content from php | Webs Developer

May 20th, 2009 at 7:59 am

[...] this new post to his blog, Sameer shows how you can push content with the xpi content type out from your PHP [...]

Your thoughts

Sign up for fresh content in your email