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.

/* 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 */

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

3 thoughts on “Pushing xpi mime content from php

  1. 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

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>