下载APK文件时的强制下载头信息

3

我正在编写一个脚本,用于增加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 字节”,基本上它没有下载文件内容。

我可能做错了什么?有解决方案吗?

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

);


确保file_get_contents($file)可以读取文件,在没有任何头信息的情况下进行测试。 - Shahrokhian
2个回答

0

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

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

然后在getapk.php上做如下操作:

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

当然这还留下了很多细节,但如果您需要任何其他帮助,我很乐意提供更多细节。


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);

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接