Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a file then redirect to another page in php?

Tags:

html

php

download

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?

like image 931
ByronArn Avatar asked Apr 08 '13 06:04

ByronArn


People also ask

How automatically redirect to another page in PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

How do I set PHP to download to a specific directory?

Create cURL session. Declare a variable and store the directory name where the downloaded file will save. Use the basename() function to return the file basename if the file path is provided as a parameter. Save the file to the given location.

How redirect same page after submit in PHP?

You should redirect with a location header after every post, because otherwise when the user presses the refresh button, it will send again the same form... Btw. if you want to do proper work, you should try out a php framework instead of this kind of spaghetti code...


2 Answers

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

like image 121
Andrew Leap Avatar answered Sep 17 '22 02:09

Andrew Leap


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.

like image 31
Nurbol Sarsenbayev Avatar answered Sep 18 '22 02:09

Nurbol Sarsenbayev