8

Okay, I have the following php file:

    <?php
        header("Content-Type: application/octet-stream");

    $file = $_GET["file"];
    header("Content-Disposition: attachment; filename=" . urlencode($file));   
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");            
    header("Content-Length: " . filesize($file));
    $file = "pdf/".$file;
    flush(); // this doesn't really matter.
    $fp = fopen($file, "r");
    while (!feof($fp)) {
        echo fread($fp, 65536);
        flush(); // this is essential for large downloads
    }

    if ($file="pdf/sampleFile.pdf") {
    ?>
<html>
<head>
    <title>Best Recipes Ever!</title>
    <link rel="stylesheet" href="style/style.css" />
</head>

<body>
    <div id="header">
        <h1>Best Recipes Ever!</h1>
    </div>
    <div id="page">
        <h1>Thank You For Your Download!</h1>
        <p>I sincerely hope that you enjoy this free recipe! If satisfied, please feel free to return and purchase some of my recipes. Trust me, you won't regret it!</p>
    </div>
    </body>
    </html>
    <?php 
    }

    fclose($fp);
    ?>

The idea is, if its the sample file, it downloads the file then redirects to a thank you page. However, if its a paid-for download(s), this file only downloads the files, but does nothing (because the page they are coming from is a list of purchased download file links, so it needs to stay on that page).

What this page is doing, however, is downloading the file but doesn't redirect. What am I doing wrong? And how can I fix it?

ByronArn
  • 215
  • 1
  • 2
  • 5

4 Answers4

9

Best bet is to turn your page order around a little bit. Set up a page that's a "thank you for downloading this sample" page, and have it set up to do a javascript refresh that actually takes you to the download. As it's a download, not a new page, the thank you page will still be there. In case they don't have javascript on the browser, put a link to the actual file.

You could have a page for each file, but best bet would be to pass the filename in as a get var, i.e. ThankYou.php?file=sample.pdf

Andrew Leap
  • 956
  • 4
  • 9
7

This code worked in my situation well. Maybe it will help you. It is main page:

<form action="/download.php" method="post">
  <button type="submit">Download</button>
</form>

This script in main page:

$(document).on('submit', 'form', function() {
  setTimeout(function() {
    window.location = "/thanks.html";
  }, 1000);
});

You must write your php code for download in dowload.php. If in download.php your code only downloads file, then your main page will not be reloaded. So the script which handles form will work.

  • 1
    This is not bad, but your 1 second timeout assumes that the download will start in at most 1 second after the button has been clicked. But it may take longer. In this case nothing will be downloaded because the page will redirect to the new location before the download starts. – Max May 28 '20 at 19:24
2

add a header() function to redirect your page after download success

header( "refresh:5;url=yourlocation.php" );

Resource link(here)

Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28
  • 1
    HTTP headers have to come before the body of the response (which is where the file would have to be). (Also the `Location` header expects an absolute URI) – Quentin Apr 08 '13 at 06:26
  • 1
    I already tried that but it didn't rediret either. Its because the header function has to be before any echo, and there's an echo in the while loop... – ByronArn Apr 08 '13 at 06:30
  • 1
    @Quentin oh i see headers are already declared .. but could it still be possible to redirect after header is sent using `header( "refresh:5;url=wherever.php" );` – Jhonathan H. Apr 08 '13 at 06:33
  • 1
    @are u sure mate?..hmm. even turning output buffering on? – Jhonathan H. Apr 08 '13 at 08:22
  • 1
    Yes, I'm sure. Output buffering wouldn't help because I set the headers before outputting any content. – Quentin Apr 08 '13 at 08:39
  • oh i see thanks for the info. would think other pssible solution for now. – Jhonathan H. Apr 08 '13 at 08:41
-1

Use html or javascript redirects, because it works even if headers were already sent. Just echo this if the download is a sample file.

e.g.

HTML

echo "<meta http-equiv = 'Refresh' content = '2; url =./redirectpage.php'/>"; // change redirectpage.php to where you want to redirect.

JavaScript

echo '<script type="text/javascript">
          window.location.href="./redirectpage.php";
      </script>';
TravellingGeek
  • 1,581
  • 11
  • 16
  • It might work if the response was loading an inline HTML document … but it isn't, it is a PDF attackment. – Quentin Apr 08 '13 at 08:17