Making MHT Single Page Archive Files with PHP

Dec
03
2006

I was digging through my "experiments" directory and found something I barely remember writing. However, it's kind of useful for some stuff I'm doing in the next couple of weeks. And, like lots of the rest of the contents of those experiments, I figured I'd share.

You may or may not know that if you are using Internet Explorer and you want to save a copy of a web page, you've got several options. You can just save the HTML, which will not look right if you open it later, but will have the text at least, a "complete" page, which saves a directory full of CSS and images next to the HTML file and an "all-in-one" file, which is an MHT file. Basically, an MHT is the HTML file and a bunch of MIME-encoded attachments for the images, etc.

The net result is a single file that contains all of the text and images for a web page. I wanted (at least according to the comments in the file) to have a way to quickly get an MHT file for a given URL without having to load it manually. I grabbed VBScript and ported it to PHP. So, what follows is PHP code for Windows and intended for the commandline.

For the sake of explanation, I'm showing the filename and URL as regular variables. To use it in more of an ongoing way, you should change those 2 variables to get their values from the commandline, using the $argv array.

If you name this file as mhtmaker.php, and you run it as "php.exe mhtmaker.php filename url", then $argv[1] will contain the filename and $argv[2] will contain the url.


$adSaveCreateNotExist = 1;
$adSaveCreateOverWrite = 2;
$adTypeBinary = 1;
$adTypeText = 2;
$URL = "http://www.wynia.org/wordpress/";
$output_file = "wyniaorg.mht";
$objMessage = new COM("CDO.Message") or die("Cannot create object");
$objMessage->CreateMHTMLBody($URL);
$Strm = new COM("ADODB.Stream");
$Strm->Type = 2;
$Strm->Charset = "US-ASCII";
$Strm->Open();
$Dsk = $objMessage->DataSource;
$Dsk->SaveToObject($Strm, "_Stream");
$Strm->SaveToFile($output_file, 2);

 

Comments on this post

Feedback is always welcome. Read some from other folks or leave your own below. Just keep things civil and remember that what you post lives on in public. Forever.

Thanks,
J

Leave Your Own Comment

By submitting a comment, you agree to license it under the terms of the Creative Commons Attribution license.

People who post comments get the added benefit of visiting the site without advertising.

© 2003-2009 J Wynia. All original content is licensed under the terms of the Creative Commons Attribution license unless otherwise noted. Content from other sources is licensed under its original terms.