When trying to host an APK file on my server I was able to get it to download fine – but I was not able to get the file size to show in the download – including showing the download percentage.

To fix this problem we need to do the steps below:

1. Disable GZIP compression for APK files, the easiest is to set it in the Apache – httpd.conf file

2. Add APK as a file type in .htaccess for the specific server

3. For PHP download you might also need to set the appropriate headers

1. In httpd.conf find and add apk – I added it after the RAR type

# Don't compress files that are already compressed
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar|apk)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.(?:avi|mov|mp3|mp4|rm|flv|swf|mp?g)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary

I also read that you could simple add the following line to .htaccess in your server if you did not have access to the Apache server config – but I did not test this

<FilesMatch "\.(apk)$">
SetEnv no-gzip dont-vary
</FilesMatch>

2. Then in .htaccess for the specific server add the apk as a type as below

AddType application/vnd.android.package-archive apk

3. Set appropriate PHP headers in download scripts.

$file = '/yourlocation/application.apk'; //not public folder

$fileSize = filesize($file);

         if (file_exists($file)) {
             header('Content-Description: File Transfer');
             header('Content-Type: application/vnd.android.package-archive');
             header('Content-Disposition: attachment; filename='.basename($file));
             header('Content-Transfer-Encoding: binary');
             header('Expires: 0');
             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
             header('Pragma: public');
             header('Content-Length: ' . $fileSize);
             ob_clean();
             flush();
             readfile($file);
             exit;
         }
SOLVED: APK download file size not shown – no download percentage – SetEnv – htaccess – PHP Content-Length – Apache Server – CentOS
Tagged on:                                                                                     

Leave a Reply

Your email address will not be published. Required fields are marked *

One thought on “SOLVED: APK download file size not shown – no download percentage – SetEnv – htaccess – PHP Content-Length – Apache Server – CentOS