0

I am trying to download an Android APK file that is output by a php page. I have the following and it works on firefox but not on the phone.

Firefox downloads with an apk extension but has a little firefox icon next to it.

The phone downloads the file with a .html extension, why is this?

UPDATE: Full source

 function display($tpl = null) {
  //SETUP
  $appId = JRequest::getInt('id', '0');
  $model = &$this->getModel();

  $app = $model->getApplication($appId);


  if( !$app )
   JError::raiseError(500, "Invalid Application ID");

  if( empty($app->apk_file) )
   JError::raiseError(500, "No APK file found in the database");


  $result = $model->newDownload($appId);

  //Update the database
  if( !$result )
   JError::raiseError(500, "Unable to increment download count for ID:".$appId);


  //Return the file
  $filesFolder = JPATH_COMPONENT_ADMINISTRATOR .DS. 'uploads' .DS. $appId;

  //Output the file contents
  $sanitizedFolder = JFolder::makeSafe($filesFolder);
  $sanitizedFile = JFile::makeSafe($app->apk_file);
  $path = $sanitizedFolder .DS. $sanitizedFile;

  if( !JFile::exists($path) ) {
   JError::raiseError(500, 'File does not exist');
  }
  else {

   header('Content-type: application/vnd.android.package-archive');
   header('Content-Disposition: attachment; filename="'.$sanitizedFile.'"');
   readfile($path);
  }
 }

My setup:

Fedora Core 10

PHP 5.2.9

Apache

jax
  • 37,735
  • 57
  • 182
  • 278

1 Answers1

6

The following works fine for me on a Nexus One running Android 2.2, as well as in Chrome:

<?php
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename="Foo.apk"');
readfile('Foo.apk');
?>

Running PHP Version 5.2.4-2ubuntu5.6.

If you provide more details on your setup, we may be able to help more.

Roman Nurik
  • 29,665
  • 7
  • 84
  • 82
  • I changed it to exactly the same as your version using readFile ect but it is still getting the .html attached to the end. – jax Jul 30 '10 at 06:33
  • I added the full source to the question. This is actually coming from a Joomla 1.5 Component. – jax Jul 30 '10 at 06:36