How do I create a multipart HTML document with PHP? I want to display a webpage and have a file download.

I've tried the following simple code to no avail:
<?php

header("HTTP/1.0 200 OK");
header("Content-type: multipart/mixed;boundary=foo");
header("Content-type: text/plain");

echo "--foo";

session_start();
$y = 5;

for ($x=0; $x<$y; $x++) {
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>custom view</title>
<meta name="generator" content="BBEdit 6.5.2" />
</head>
<body bgcolor="#ffffff">

</body>
</html>
<?
echo "<h3>[ $x ]</h3>";
echo "--foo";
}
?>

    multipart html ????? never heard of something like this

    maybe you are looking for multi-framed html??
    if so... just use old good html frames

      Yes, multipart.

      I want to have a page load that says something to the effect of "if the file doesn't begin downloading, click here." Then in a couple of seconds, start the download.

        That's not a multi-part.

        That's a META REFRESH redirect with a specially-formatted HTML page which can be generated with PHP.

          I don't want a META tag. That would refresh the page and take me to another URL. Try downloading something from download.com or versiontracker.com. They are not using the META REFRESH.

          Read http://tony.radio-esperanto.com/web_docs/xhtml/ch13_03.htm It describes creating the form, and has a sample shell script to do it. That script works, but I'm not sure how to deal correctly with creating all the header information in PHP.

            OK, so I was wrong about how download.com does it, but my original problem is still unsolved. How do I create a multipart HTML document in PHP?

              so you want two pages shown at once.. one page saying the blah blah stuff to the user (including if download doesn't start click here) and another page to download the file? if i got it right... you could make some magic by combining PHP and javascript. So you make one page that mentions the "blah blah" stuff which has an OnLoad event in its <body> tag. This event triggers another page that deals with the auto-download which also has an OnLoad event. When this page finished its work its OnLoad event will close the window. Practically the second window will show for a few msec, so the user will never get to see it..... it that of any help?

                No, I'm not looking for a work around. I know there are many of these. If you look at the documentation at http://tony.radio-esperanto.com/web_docs/xhtml/ch13_03.htm . There you will see information (from O'Reilly) about server-push and multipart/mixed media. Near the bottom of the page is some sample code (sh to be specific). I have tested that code (with some very minor changes) and have it working at [URL=ttp://dev.thespot.uc.edu/cgi-bin/pushtest]pushtest[/URL].

                I want to know how to implement the same concept in PHP. The sample code I have in the first post in this thread isn't building the headers properly. See it not working here. It's not rendering out as HTML, but displaying it. How do I get the headers correct in PHP so it renders?

                Why all this trouble? Because I'm generating a .vcs file and don't want to store it as a file on my HD, but rather dynamically generate a pretty table for the visitor in HTML and give them the file. So, unless I write another page and query the database again, none of the other options mentioned work. Since I already have the data from the query, I just want to push it to them in a different format.

                  Ugh, this is so silly.

                  We're going in circles and you are dogmatic about doing it one way only instead of just telling us what you need and asking us how to do it.

                  First of all, trying to do it with all the cute automatic stuff like download.com just makes it harder.

                  Figure out the file download FIRST, then make it fancy later.

                  Server push is NOT needed for this task. Anyway if you really want server push, PHP does it out of the box with buffering ob_start(), ob_flush(), etc.

                  Why all this trouble? Because I'm generating a .vcs file and don't want to store it as a file on my HD...but rather dynamically generate a pretty table for the visitor in HTML and give them the file. So, unless I write another page and query the database again, none of the other options mentioned work. Since I already have the data from the query, I just want to push it to them in a different format.

                  I'm confused. Are you making VCS or HTML?

                  If you are making VCS, then you can do that easily with the process below. If you are making HTML, just tell the user to pull down the File menu to "Save As...".

                  To make a VCS file, there is absolutely nothing preventing you from taking the results of the query, stuffing it into a string, and passing it to a "download" page with $_POST. There is no need to re-perform the query on the download page.

                  Looking up VCS on the internet, I found an example VCS file here:
                  http://www.dominoconsulting.net/Stars2002/

                  VCS is just a plain text database format.

                  If you used the search engine, you would have found this article right here on phpbuilder.com. Ugh!:
                  http://www.phpbuilder.com/columns/chow20021007.php3?print_mode=1

                  Here's what I'd do:

                  your_current_page.php

                  <?php
                  
                  // do database query
                  // and stuff into $data
                  
                  ?>
                  
                  <form action="download_vcs.php" method="post">
                  <input type="hidden" name="data" value="<?php echo $data; ?>">
                  <input type="submit" name="submit" value="Download">
                  </form>

                  download_vcs.php

                  <?php
                      header("Content-Type: text/x-vCalendar");
                      header("Content-Disposition: inline; filename=MyvCalFile.vcs");
                      echo $_POST['data'];
                  ?>

                    Just a couple of observations made in passing.

                    [1]
                    A couple of points that may not be apparent in the shell script, but are covered in the MIME standard (RFC1341):

                    "Each part starts with an encapsulation boundary, and then contains a body part consisting of header area, a blank line, and a body area."

                    ".... NO header fields are actually required in body parts. A body part that starts with a blank line, therefore, is allowed and is a body part for which all default values are to be assumed. In such a case, the absence of a Content-Type header field implies that the encapsulation is plain US-ASCII text. The only header fields that have defined meaning for body parts are those the names of which begin with "Content-".

                    You're missing that blank line between parts (echo "" in the shell script). Also, since you want the Content-type to change, you'll need to specify an appropriate Content-type header for each part (which hence have to be output as part of the content, instead of via header(); notice that in the shell script "text/html" is inside the loop). Consider: saying that the content type is "multipart/mixed" and then immediately saying that it's "text/plain" looks a bit odd.

                    [3]
                    Also, the question of whether the client will know what to do with multipart messages with different types does depend on the client.

                    [4]
                    And of course, unless you're using output buffering, you can't go and call session_start() once you've starting outputting page content (like "-foo").

                      Write a Reply...