Originally published on: 12/3/2006 7:06:26 PM
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);