dsc56927 2012-12-14 21:03
浏览 74
已采纳

强制下载apk文件的标题

I'm doing a script that increases the counter for an APK file's download then sends the file to the browser for download.

Here's what I have:

<?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

The problem is that with a browser like firefox, it downloads but it is like 'android.apk - 0 bytes'. So it essentially, it does not download the file's contents.

What might I be doing wrong? A solution for this?

IMPORTANT: It has to work on mobile.

);

  • 写回答

2条回答 默认 最新

  • dongqiuge5435 2012-12-14 22:46
    关注

    I've realized that I don't need to use complex header info expecially if the script will be moved from server to server where the .apk mime type is not native and may therefore be hard for a novice to set up.

    A simple redirect will do:

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

    Voila! I have increased the counter and the download will be pushed to the browser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题