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
https://forums.phpfreaks.com/topic/185197-downloading-a-file-as-html/
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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.