0

I am trying to create link to save browser output as file without creating file on server.

This is what I got so far:

<?php
ob_start();
?>

<html>
    webpage content
</html>

<?php
$page = ob_get_contents();
ob_end_flush();

$file= time().'.html';

file_put_contents($file, $page);
ob_start();
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
ob_end_flush();
?>

<a href="<?php echo $file; ?>.php">Download output as file</a>

How can i create such link WITHOUT SAVING file on server?

Thank you for your suggestions/ideas/code.

AD7six
  • 63,116
  • 12
  • 91
  • 123
Ing. Michal Hudak
  • 5,338
  • 11
  • 60
  • 91
  • Try using all caps, that helps. – Anthony May 05 '14 at 08:29
  • @Anthony I was just pointing out important note. – Ing. Michal Hudak May 05 '14 at 08:30
  • Allready answered here: http://stackoverflow.com/questions/17190224/take-a-screenshot-of-the-current-screen-and-save-it-as-a-png-image-through-php Note: you can't do this with php because php is executed on the server before the actual html is sent to the browser. When the browser receives the html, it renders it on the screen. – user1498339 May 05 '14 at 08:30
  • 1
    It can be done, but not in the way you're trying for the reason already mentioned. But since your output already exists before page is requested, you could use data URL for href value. – Anthony May 05 '14 at 08:39

4 Answers4

2

Why that complicated? Do it straight forward instead:

<?php
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename="'.time().'.html"');
?>
<html>
    webpage content
</html>
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • I want to create LINK, to dowload currecnt output. To make it optional. – Ing. Michal Hudak May 05 '14 at 08:31
  • Sure, create a link to the script. Anyone who clicks the link will be offered the download. It is simply _impossible_ to first create content but only send out a link, then react on a click on that link and send out the previously created content _without_ storing it inbetween. Note that the clicked link is a completely independent http request. That is, you _might_ actually be able to save the created content inside a session, but that is a horrible architecture! – arkascha May 05 '14 at 08:32
1

You have two fairly easy options (there are others, but they would be more complicated) :

Option 1, use a data url:

$pageData = base64_encode($page);
$finfo = new finfo(FILEINFO_MIME);
$pageDataMime = $finfo->buffer($page);
$pageDataURL = 'data:' . $pageDataMime .  ';base64,'.$pageData;
?>

<a href="<?php echo $pageDataURL; ?>.php">Download output as file</a>

Option 2, use query string to determine if output should be downloaded or not:

if($_GET['download_data']) {
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Content-Length: ' . filesize($file));
    echo $page;
    exit();
} else {
    // Output HTML as normal, including:
    <a href="<?php echo $normalPageURL ?>?download_data=1">Download output as file</a>
}
Anthony
  • 36,459
  • 25
  • 97
  • 163
0

Open a file handle like this fopen('php://output', 'w'); and output to it.

Klemen Tusar
  • 9,261
  • 4
  • 31
  • 28
0

Just echo $page:

<?php
ob_start();
?>

<html>
    webpage content
</html>

<?php
$page = ob_get_contents();
ob_end_flush();

$file= time().'.html';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . strlen($page));
echo $page;
?>
Zebrastorm
  • 81
  • 1
  • 4