gerkintrigg Posted December 15, 2009 Share Posted December 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
steveboj Posted December 15, 2009 Share Posted December 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted December 17, 2009 Author Share Posted December 17, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.