Jump to content

downloading a file as HTML


gerkintrigg

Recommended Posts

Hi.

I'm rather confused with forcing a download. I just want to save dynamic content (from $_SESSION[''] data) as a .html file.

 

The script to do this is a PHP page, but when I click on "download" it downloads as "download.php"

 

Here's the code:

header("Cache-control: private"); //IE 6 Fix 

if (isset($_GET['download']) && $_GET['download']=="true" && isset($_GET['filename']) && $_GET['filename']!="") { 
     
    // retrieve content of the file here, e.g. from a database... 
     
    $content=$_SESSION['old_page']; 
     
    // send the header here 
    header('Content-type: text/plain'); 
    header('Content-Disposition: attachment; filename="' . $_GET['filename']); 
     
    // put the content in the file 
    echo($content); 
     
    // stop processing the page 
    exit; 
}	

 

It's accessed using:

<a href="../outputs/download_as_file.html?download=true&filename=shine_output.html">Download</a>

 

Any suggestions? I found a good tutorial here: http://richardlynch.blogspot.com/2006/06/php-downloads-content-disposition.html but don't think it'll do what I need to do.

 

Link to comment
Share on other sites

I think I had this problem with a script I wrote a few years ago. I've just had another look at it to see if I could remember what solved it.

 

1. My script used:

 

header('Content-Type: application/octet-stream');

 

If that doesn't help, you could try this:

 

2. My script first saved a copy of the file on the server. Then it used "readfile()" to output the file, and once that file had been downloaded, the copy on the server was deleted.

If you want to give that a go, take a look at the manual page for readfile() because it includes a useful example = http://uk3.php.net/manual/en/function.readfile.php.

Link to comment
Share on other sites

Eventually I did this:

<?php
// start the session 
session_start(); 
$root='';
header("Cache-control: private"); //IE 6 Fix 


//////////////////////////////////////////////////////////////////////////////// 
// Download code 
//////////////////////////////////////////////////////////////////////////////// 
if (isset($_GET['download']) && $_GET['download']=="true" && isset($_GET['filename']) && $_GET['filename']!="") { 
     
    // retrieve content of the file here, e.g. from a database... 
     
    $content=$_SESSION['output_page']; 
     
    // send the header here 
header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $_GET['filename'].'"'); 
     
    // put the content in the file 
    echo($content); 
     
    // stop processing the page 
    exit; 
}	
//////////////////////////////////////////////////////////////////////////////// 
// End of the download code 
//////////////////////////////////////////////////////////////////////////////// 
?>

I include this into the page I want to download from. It outputs the session variable (containing HTML) as any file name I want, which I parse using a GET variable in a href tag like this:

<a href="../outputs/download_as_file.php?download=true&filename=shine_output.html">download</a>

 

The issue I had was solved by ending the

header('Content-Disposition: attachment; filename="' . $_GET['filename']

with

.'"'); 

 

Cheers.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.