MHT to HTML Converter
PHP Script

This page documents a PHP script I have written to convert Word generated mht (single file webpage) files to the html file, images, and other component parts.  I maintain a web site where I have users who needed to be able to upload and change their own page.  Like most users they were proficient in Microsoft Word but knew very little about html or how to FTP the files and images needed to update a page.  I had an idea that they could save their document from Word  as a single file webpage (mht file) and upload it through a PHP form.  Once uploaded a PHP script could process the file and write the component parts to the correct folders.

I set out to find a PHP script that would parse the mht file and found a class that was supposed to do the job.  I downloaded and worked with it for a while but never got it to work properly.  I also thought the code was somewhat convoluted and over complicated.  An mht file is, after all, a multipart message file that is intended to be parsed.  It should not be super difficult to do.  After extensively searching for other scripts and failing to find anything, I decided it was time to create my own.  The script below is a result of that effort.

To use the script below, paste it to a text file named mht2html.php and copy it to a folder on your server.  Place a valid myfile.mht file in the same folder then browse to the script on your site.  When the script executes it will parse myfile.mht into myfile.htm and create a folder called myfile_files which will contain the image, xml, and other files.  Once the files are created, myfile.htm can be viewed in any browser and should appear just as it did in Microsoft Word.  You can change the input filename and the output folder by editing the first two lines in the file.

This has been tested with mht files created in Word 2016.  I have no idea if it will parse older versions but I suspect it will or can be modified to do so.  It is posted here because I think there are probably other web designers who can use this functionality and there doesn't appear to be many other working and open PHP scripts around.  Test it out, use it, modify it, implement your own upload page and enjoy.  If you want to save some work designing an upload form and turning this into a form processing script then see the bottom of this page for a cheap way to obtain the code to do it all.
The code...


Make it easy to make it all work. Get the uploader!

In order to use this core converter code on a website I needed an upload page and a PHP script to process the uploaded .mht file.  It took a few hours of extra work to get it all going and I have decided to offer that work here for others.  I have even created a publicly available converter demonstration page to allow perspective users to see and test the code.  I recommend that you go to the demo page and give it a try.  This will insure that it will do the job you need before you spend a fortune obtaining the code.  The demo page is here.  If it works for you, read on...

THE DEMO WILL ONLY WORK FOR MHT FILES CREATED WITH WORD 2016.  If that is not the program used to create the file, don't waste your time uploading it.

I offer most of the code on this site free and allow it to be downloaded directly from the site.  All I ask for in return is an unspecified small donation.  The problem is, I get lots of downloads but very few donations.  I put a lot of time into developing this mht to php converter code and the html upload form so I am using a different method for delivering it to the public.  For a PayPal donation of just $10.00, which is a processing fee, I will email you the code and documentation as a zip file attachment.  The zip file includes an html page for uploading, the php script to process the form data and parse the mht file, and a text file with details for using the two files on your website.  Make sure your PayPal account has a valid email address attached or use the PayPal instructions box to let me know where to send the code.

I will NOT spam you.  
I will not even keep a record of you beyond what PayPal keeps.  The code is free and you are simply paying a processing fee.  You are not registering, subscribing, licensing, buying support, or anything like that, you are just paying me to send you the code. You are free to use and modify the code as you see fit with the exception of redistributing or selling it.  I retain the copyrights on the original work.


Questions / Comments?  Email me.

MRE Logo