Malaiselvan
Malaiselvan

Reputation: 1125

Download APK file using PHP

I am trying to download an android APK file using php in my PC browser using Chrome.

My app is located in a particular path in the server. If I manually FTP the file from server and transfer to my android mobile it installed perfect. But when I downloaded using PHP and transfer the downloaded file to Mobile and while installing it throws 'there was a problem while parsing the package'.

Here is my PHP code I use to download

header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename="' . $file_name . '"');
readfile($file_path);
return true;

fyi...

$file_name is the apk file file 'myfile.apk'
$file_path is the full absolute path of the file in server ('d:\abcd\xyz\xampp\htdocs\apkstore\myfile.apk')

I found one observation while trying to open the APK file using 7-zip.

When I open the file using 7-zip it throws an error 'Cannot open the file xxx as archive'

After I added the below PHP code

header("Content-length: " . filesize($file_path));

Now when I open the file using 7-zip it opens the file but the size of the downloaded file is greater than the original file. And when I open this file in mobile the same error 'there was a problem while parsing the package'

To cut my long story short, I am trying to download an APK file from server to localhost using PHP and I'm able to make it work.

Upvotes: 0

Views: 10005

Answers (1)

Malaiselvan
Malaiselvan

Reputation: 1125

I managed to make it work by adding ob_end_flush()

header('Content-Type: application/vnd.android.package-archive');
header("Content-length: " . filesize($file_path));
header('Content-Disposition: attachment; filename="' . $file_name . '"');
ob_end_flush();
readfile($file_path);
return true;

Upvotes: 2

Related Questions