Search code examples
javascriptphpandroidapachelamp

Garbage output while downloading apk from server using PHP


I'm using PHP to compile an app on an apache server based on the input provided by the user and then provide the app to them using the readfile() method.
The problem that I'm facing is that instead of actually downloading the app, the .apk file is being opened as a text file.
enter image description here

I have added the android app mime type in the PHP code.
My php code is :

<?php
echo "Please wait while the app is being generated, it can take around 10 minutes for the build to finish.The completed app will be mailed to you ";

if(isset($_POST['timestamp']))
{
$uid = $_POST['timestamp'];
exec("sudo python /var/www/html/appgenserver.py $uid");

header("Content-Description: File Transfer");
header("Content-Type: application/vnd.android.package-archive");
header("Content-Transfer-Encoding: Binary");        //Adding or removing this hass no effect
header('Content-Disposition: attachment; filename="Foo.apk"');
ob_clean();
flush();
readfile("/var/www/html/".$uid."/releaseapk.apk");
exit();
}
?>

Any help would be appreciated.


Solution

  • So what I did was, rather than return the app from PHP script, I returned it URL to the AJAX call.

    From there I navigated to that URL and the download initiated automatically.

    PHP code :

    <?php
    {
        echo "http://server_ip/release/".$uid."/releaseapk.apk";
    }
    ?>
    

    Ajax cal :

    $.ajax({
            type: "POST",
        url: "/test.php",
        data: { timestamp : timestamp },
            success: function(response){
              console.log("Success",response);
              window.location = response;
        }
          });