强制下载apk文件的标题

时间:2012-12-14 21:03:40

标签: php android header download apk

我正在编写一个脚本,增加APK文件下载的计数器,然后将文件发送到浏览器进行下载。

这就是我所拥有的:

<?php
    $file = "android.apk";

    function force_download($file){
    header("Pragma: public", true);
    header("Expires: 0"); // set expiration time
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Disposition: attachment; filename=".basename($file));
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($file));
    die(file_get_contents($file));
    }

    force_download($file

问题是,使用类似Firefox的浏览器,它会下载,但它就像'android.apk - 0 bytes'。所以从本质上讲,它不会下载文件的内容。

我可能做错了什么?解决这个问题?

重要:必须在移动设备上运行。

);

2 个答案:

答案 0 :(得分:0)

我从来没有访问过强制下载的.apk链接,所以我不确定强制下载是什么需要。至于递增计数器,我可能只是链接到一个页面,该页面在计数器完成后转发到apk文件。

例如将某人链接到:getapk.php?apkid = 1

然后在getapk.php上做这样的事情:

 $update = mysql_query("UPDATE apps SET downloads...");
 if ( $update ) { header("Location: appname.apk"); }

当然会遗漏很多细节,但如果您需要其他任何帮助,我很乐意提供更多细节。

答案 1 :(得分:0)

我已经意识到我不需要使用复杂的头信息,特别是如果脚本将从服务器移动到服务器,其中.apk mime类型不是本机的,因此可能很难设置新手。

简单的重定向即可:

$file_name = $_GET['f']; //$_GET['f'] has the link to the file like http://mydomain.com/file/android.apk


//Do database query or increase download counter

header('location: '.$file_name);

瞧!我已经增加了计数器,下载将被推送到浏览器。

相关问题