2

I need to download an apk application but I need to keep the user in my page or redirect him to another page with header I can only download the application or redirect him to another page, but I can't do both at the same time my php code :

    $b= "location"; 



    echo "<script>window.location.href = '$b';</script>" ; 

            $file = 'apk.apk';

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

}
Rami Osman
  • 137
  • 1
  • 13

2 Answers2

1

According to my research for 5 minutes. what you are requesting is impossible.

Take as an example a page that downloads something then redirects to "thank you for downloading"

The way they actually do it is they take you to the "thank you" page, and then they start the download.

To implement this in your code, you'd need to open up $b location first, and in that script you start the download by redirecting to a PHP script that contains the downloading.

Example:

download.php

$file = 'apk.apk';

if (file_exists($file))
{
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
}

hello.php

$b= "download.php"; 
echo "Hello! <script>window.location.href = '$b';</script>" ; 

When you visit hello.php you will see Hello! and the download will start

pouyada
  • 341
  • 3
  • 15
Cârnăciov
  • 1,169
  • 1
  • 12
  • 24
1

@aron9forever thanks for your help, I found a good way to do that :

$e= "url.com" ; 



echo "<script>setTimeout(function(){window.location.href = '$e';} ,2)</script>" ; 


echo "<iframe src='download.php' ></iframe>" ; 

and the download.php :

<?php
$file = 'apk1.apk';

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

}
?>
Rami Osman
  • 137
  • 1
  • 13