Force Download?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moishy
    New Member
    • Oct 2006
    • 104

    Force Download?

    How do you make a "Force Download" i.e. that instead of for instance having a mp3 file open in a new tab, rather it will prompt the download.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Use content disposition for this.
    [php]
    <?php
    // We'll be outputting a PDF
    header('Content-type: application/pdf');

    // It will be called downloaded.pdf
    header('Content-Disposition: attachment; filename="downl oaded.pdf"');

    // The PDF source is in original.pdf
    readfile('origi nal.pdf');
    ?>
    [/php]
    Taken from php.net/header

    Take a look at this too.

    Comment

    • moishy
      New Member
      • Oct 2006
      • 104

      #3
      Thanks!
      It works!
      One more question:
      In the page you linked to in the above post, is says that the file may not contain blank lines before or after code.
      My question is:

      <?php
      // May I place code here? if not, is there anywhere in this PHP tag that I may?
      // May I place blank lines anywhere IN the PHP tags?
      header('Content-disposition: attachment; filename=movie. mpg');
      header('Content-type: video/mpeg');
      readfile('movie .mpg');
      ?>

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by moishy
        Thanks!
        It works!
        One more question:
        In the page you linked to in the above post, is says that the file may not contain blank lines before or after code.
        My question is:

        <?php
        // May I place code here? if not, is there anywhere in this PHP tag that I may?
        // May I place blank lines anywhere IN the PHP tags?
        header('Content-disposition: attachment; filename=movie. mpg');
        header('Content-type: video/mpeg');
        readfile('movie .mpg');
        ?>
        I believe you may place code that does not produce any output before the headers.

        Experiment with it!

        Comment

        Working...