0

I need to write and create directly on the page, a .html file and than let the user download it to his local machine. The .html file will be full os information from database requests. It will look something like this:

<?php
    $content = "<!DOCTYPE html><html lang="en"><head></head><body>";
    $content .= "<div>Hello World</div>";
    $content .= get_all_contents_from_db();
    $content .= "</body></html>";
?>

I saw a code that lets you download the page you are seeing into a file with this code:

    <?php 
    if(isset($_POST['download']))
    {
        header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
        header("Content-Type: application/force-download");
        header("Content-Length: " . filesize($File));
        header("Connection: close");
    }
    ?>
So the question is: How do I create a downloadable html file using php?

**PART 2**
<form method="POST" action=''>
    <button name="download">Download File</button>
</form>
<?php 

    $content = "<!DOCTYPE html><html lang=\"en\"><head></head><body>";
    $content .= "<div>Hello World</div>";
    $content .= "</body></html>";

    if(isset($_POST['download'])){

        echo $content;
        header("Content-Disposition: attachment; filename=\"slideshow.html\"");
        header("Content-Length: " . filesize($content));
        echo $content;
    }
?>

Output

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
    </head>
    <body>
    <div class="main">Dashboard</br>
        <form method="POST" action=''>
            <button name="download">Download File</button>
        </form>
        <!DOCTYPE html><html lang="en"><head></head><body><div>Hello World</div></body></html><!DOCTYPE html><html lang="en"><head></head><body><div>Hello World</div></body></html>            </div>
    </div>
    </body>
</html>
CIRCLE
  • 3,695
  • 3
  • 32
  • 51
  • 2
    You question is lacking a question. – PeeHaa Aug 09 '13 at 09:30
  • 1
    Also `application/force-download` is a nonexistent content type. – PeeHaa Aug 09 '13 at 09:31
  • The Content-type for HTML documents is `text/html`. – Quentin Aug 09 '13 at 09:31
  • `application/force-download` may be a nonexistent content type but it has been used as a hack; see for example http://stackoverflow.com/a/10616753/469210 – borrible Aug 09 '13 at 09:33
  • 3
    You don't need to use a hack. That's what Content-Disposition is for. – Quentin Aug 09 '13 at 09:33
  • I want to take the var $content and whitin its text, generate a html file and download to the local machine – CIRCLE Aug 09 '13 at 09:35
  • @borrible correct it has been **abused** as a hack although there is no reason to use it. – PeeHaa Aug 09 '13 at 09:37
  • After sending those headers, you have to echo the contents of the file (or string you wish to send). Add it there and it should work. – N.B. Aug 09 '13 at 09:47
  • Dont forget after your `header` list to add something like `readfile`: header('Content-Type: blabla'); // ... // Outputs file readfile($File); – rap-2-h Aug 09 '13 at 09:45

2 Answers2

3
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");

Fix this so you specify the filename from somewhere sensible (instead of trying to compute it off the name of a file that doesn't exist on your hard disk from an undefined variable).

header("Content-Type: application/force-download");

Get rid of this. It is a dirty hack for people who don't know that Content-Disposition exists.

header("Content-Length: " . filesize($File));

If you are going to specify the content length, then you need to do so using the data in your variable, and not measure the file size of the aforementioned non-existent file.

Then just echo your $content

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • Hi Quentin, thank you for your answer! I've managed to download the file with the content from $content but when I open the file in my text editor it comes with extra tags and headers from the file it was originated. How can I download the file only with the text contained in the var $content? I've edited the question so you can see what I have so far. – CIRCLE Aug 09 '13 at 14:07
  • You need to *not output the stuff you don't want*. The script should provide the HTML file for download and nothing else. You can't return a page to display and a file to download *at the same time*. – Quentin Aug 09 '13 at 14:12
  • Is there a better way of removing the lines of code I don't want instead of using str_replace ('all the lines to remove', '', $content)? – CIRCLE Aug 09 '13 at 14:16
  • 1
    Yes. Don't add those lines in the first place. Either split your script into two or use an if/else. – Quentin Aug 09 '13 at 14:23
  • To work with the string and split where is needed, how do I save all the content before downloading the file. Something like this may help to understand my question: $all = header("Content-Disposition: attachment; filename=\"slideshow.html\""); – CIRCLE Aug 09 '13 at 14:37
  • You shouldn't be assigning the return value of `header()` at all. If you don't want something in the string you are echoing, then don't put it in that string in the first place. – Quentin Aug 09 '13 at 14:42
  • The thing is that what i'm putting here is a sample of what i'm working on. The real .php file where the .html file is generated has infinite "includes" and menus and buttons that go attached with the downloaded html file. I need the file html only to have the tags to support html code and the string from the var $content. – CIRCLE Aug 09 '13 at 15:01
  • If you don't want the includes to be in the downloaded file, then you cannot include them (outside of an if/else) in the script that generates the download. That's not going to change. – Quentin Aug 09 '13 at 15:07
-1

First try to be make sured about Is your file which you are want generated that is to be make or not with through your code?

If is generated then go to Step 2

Else Step 1 Add the error reporting function in the first ..

          <?php
       error_reporting(E_ALL ^ E_NOTICE);
         $content = "<!DOCTYPE html><html lang="en"><head></head><body>";
         $content .= "<div>Hello World</div>";
         $content .= get_all_contents_from_db();
         $content .= "</body></html>";?>

Because Is in my case (when there was I Need them to genrate a new file through php) its add undefined error due to file was not generated Which was to be resolved with the help of error reporting function..

Step 2:

After make sure your file is originated then for downloading the file

Way to Download the file Simple way: try a href tag to simply link with that file which you want to be the downloaded

            <?php
           <a href="location of sample.html file">Sample File</a>
            ?>

Through Coding Way: (Suppose you want to download the changepwd file then it syntax is to be )

       <?php
      //$file="chngpwd.php";
     if($_REQUEST['download'])
       {
 $file="chngpwd.php";
 $fs = filesize($file);
$ft = filetype($file);

header('Content-Type:application/'.$ft);
header('Content-Disposition:attachment;filename='.$file);
header('Content-length'.$fs);
echo file_get_contents($file);
      }?>

      <form method="post" action="" enctype="multipart/form-data">
 <input type="submit" name="download" value="DOWNLOAD">

     </form>
Hdhams
  • 24
  • 5