0

I have 3 pages - the origin page with a button, the destination page, and the download page which saves a file using content-disposition - in which I need to serve the destination page after a file downloads from a button press on the origin page.

The flow of redirects should be something like:

origin page -> [button click] -> download file -> redirect to destination page

I want the user to be able to download the file and be redirected with only one press of the button. I have tried doing this in the following way, but it was to no avail:

To process the download of the image, I use the following code on the download file

<?php
header('Content-disposition: attachment; filename=calendar.jpg');
header('Content-type: image/jpeg ');
readfile($_POST['_img_src']);
?>

When the button is pressed, it directs the user to the download page and the image downloads, however it does not redirect to the destination page. After adding header('Location: /directory/mypage.php'); it nullifies the download and only redirects to the destination page due to the header conflict.

If I put the image download code at the beginning of the destination file, the download is corrupted and the content does not load.

Any help is greatly appreciated!

EDIT: The image to download was already created by the user and stored on the server. The origin page sends the image's link (by post method) to the download file, so I can not go to the destination page and then redirect to the download file as explained on here. It could be possible if there is a way to send data by post method while redirecting to the download file automatically. But I think it will make more complex the problem.

Community
  • 1
  • 1
Max Collao
  • 91
  • 1
  • 12
  • Related question for updating DB after download, please check http://stackoverflow.com/questions/1563187/check-if-download-is-completed – Rabea Jan 13 '16 at 21:35
  • 1
    Possible duplicate of [PHP generate file for download then redirect](http://stackoverflow.com/questions/822707/php-generate-file-for-download-then-redirect) – Patrick Q Jan 13 '16 at 21:37
  • This is not a duplicate because the reason explained on the edit note. Also this is not about updating DB after download or check if a download is completed. Thank you anyway for your comments. – Max Collao Jan 13 '16 at 23:08
  • @illapu the duplication can be in the close similarity of the question, as a different case of what you want to do after the download does not mean it is a new case. Your issue is understanding how PHP handles requests. – Rabea Jan 14 '16 at 13:08

3 Answers3

1

A location redirect returns a 302 redirect response to the browser. A file download returns a 200 response and the file content to the browser.

You can't do both with the same request.

You could however, have the download open in a new window, and then use JS to redirect the current page? In decent browsers, opening a download in a new window, just temporarily opens a new tab and then closes it once the download starts.

<a href="/download.php" target="_blank" id="button">Link</a>

<script>
    $(document).ready(function () {
        $('#button').click(function() {
            window.location.href="/redirect";
        });
    });
</script>
worldofjr
  • 3,868
  • 8
  • 37
  • 49
MajorCaiger
  • 1,893
  • 1
  • 12
  • 18
  • This is the best method in my opinion. Most modern browsers close download tabs/windows after a download occurs on the page so this will give you your desired result. – Hugo Pakula Jan 13 '16 at 21:38
0

What MajorCaiger wrote should work. If you don't use jquery the Javascript-Code could be

<script>
document.getElementById('button-id').onclick=function(){window.location.href='newlocation';};
</script>
bloodstix
  • 144
  • 5
0

Just a thought, not sure if works.

if(isset($_POST['__img_src'])){
$foo = new foo; // new object
}
<form action="current_page.php">
<button>... and so on
</form>

//bar.php:
class foo{
public function __construct(){
$this->download();
$this->relocate();
} // end of constructor
public function download(){} // your code in between brackets
public function relocate(){} // your code in between brackets
}

after you submit the form, it sets $_POST variable, and if post variable is set, you create a new instance of the object foo. Constructor is a function that is automatically called when the instance of an object is created. Inside constructor you call two functions from the class. Set redirrect timer to 3 seconds, or some other logic here...

JosefPP
  • 642
  • 1
  • 5
  • 11