2012-12-14 114 views
3

我正在做一个脚本,增加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字节”。所以它基本上不会下载文件的内容。

我会做什么错?这个解决方案?

重要事项:它必须在移动设备上工作。

);

+0

确保file_get_contents($ file)可以读取文件,用任何头文件测试它 – RezaSh

回答

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

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

0

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

例如链接别人:getapk.php apkid = 1

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

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

当然,留下了大量的细节,但如果你需要任何帮助,我会很乐意提供更多的细节。