I have a PHP variable $foo that contains a complete HTML page. How do I write that variable on-the-fly into a downloadble HTML file? In other words, how do I turn $foo into a link that the user clicks on and downloads foo.html?

    It's a bit fuzzy for me right now the exact use case we're looking at here. It sort of sounds like what you're asking is something in a link that would tell the target page what is supposed to be output, but that seems too obvious and something I'm pretty sure you already know how to do, so I suspect I need a few more details to understand the question. 🙂

    Or is it that you want something to be downloaded as an attachment instead of displayed in the browser? If so, [man]header[/man] would be your friend. There are some examples on that page of how to make the output into a download, so you'd just need some conditional check, maybe via a "download" parameter in the URL?

    if(isset($_GET['download'])) {
        // set download/attachment headers here
        // must be done before any output is sent to the browser
    }
    // rest of script
    

      Most of the download stuff we've done here is much like NogDog's example.

      One of the more interesting and somewhat different ones is like this: 1. On a button click, JavaScript grabs the entire content of the BODY tag and sends it to a remote server along with a verification code. 2. The remote server converts the HTML to a PDF and sends a URL back to the sending JavaScript. 3. JavaScript opens a iframe on the download URL (with hidden visibility) and the browser prompts the end-user for file download.

      I'm not sure you'd need to go quite that far for this project, based on the description so far. We had to do ours this way to comply with management requirements. Last I knew it wasn't really an optimally-working solution, either. 🙁

        Actually, what I'm doing is amassing various information from a custom shopping cart estimate tool into a PHP variable. What I'd like is to provide a link or button that when clicked on, provides the variable as a physical download. It will be delivered in an HTML format initially, but it may change to a CSV, depending on what the customer ultimately decides on.

          Maybe store a copy of the variable in $_SESSION, then access it in the download link's target script?

            Just had a thought, based upon a number of posts I've been wading through. Suppose I create a form with the PHP variable as a hidden input. When the user clicks on the button, it redirects to an automatic download of the content, using some header information. Following that, it does another form POST mechanism to redirect back to the page it came from (POST is necessary as the page is generated based upon a hidden POST input variable). Is this viable, or is there something more elegant out there?

            The downloaded file is a subset of what is on the user's screen. No headers, footers, menus. Certain table columns aren't used. Just a simple HTML file containing a single table with some totals.

              jkurrle;11060233 wrote:

              Suppose I create a form with the PHP variable as a hidden input. When the user clicks on the button, it redirects to an automatic download of the content, using some header information. Following that, it does another form POST mechanism to redirect back to the page it came from (POST is necessary as the page is generated based upon a hidden POST input variable). Is this viable, or is there something more elegant out there?

              Viable, as in, would it work? Theory's sound enough, I suppose. What would be more elegant? What ever is a] fastest "feeling" to the user, and b] easiest for the programmer and c] most accurate in its output.

              The downloaded file is a subset of what is on the user's screen. No headers, footers, menus. Certain table columns aren't used. Just a simple HTML file containing a single table with some totals.

              Makes me wonder if it could all be done in JavaScript (probably), but that isn't what this site is about, nor do I know your level of comfort there...

                Write a Reply...