【问题标题】:Headers for force download of apk file用于强制下载 apk 文件的标头
【发布时间】:2012-12-14 21:03:39
【问题描述】:

我正在编写一个脚本来增加 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”。所以它本质上不会下载文件的内容。

我可能做错了什么?有解决办法吗?

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

);

【问题讨论】:

  • 确保 file_get_contents($file) 可以读取文件,在没有任何标题的情况下对其进行测试

标签: php android header download apk


【解决方案1】:

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

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

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

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

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

【讨论】:

    【解决方案2】:

    我已经意识到我不需要使用复杂的标头信息,特别是如果脚本将从服务器移动到 .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);
    

    瞧!我增加了计数器,下载会推送到浏览器。

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多