0

How do I force the browser to download the current page browsed to? A page with the header Content-type: text/plain for example using PHP?

If a user navigates to that page, a download box should appear (the browser download dialog with the usual "Save As".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yretuta
  • 7,963
  • 17
  • 80
  • 151

1 Answers1

3

Straight from http://php.net/header

<?php
    // There is contention over if this MIME type is right, but just use it for now.
    header('Content-type: text/javascript');

    header('Content-Disposition: attachment; filename="file.js"');

    readfile('file.js'); // Echo the file
?>

NOTE: this must be done before any other output (and can be about the only thing on the page, unless you want other output in your file).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dan Beam
  • 3,632
  • 2
  • 23
  • 27