Download APK file using PHP - php

I am trying to download an android APK file using php in my PC browser using Chrome.
My app is located in a particular path in the server. If I manually FTP the file from server and transfer to my android mobile it installed perfect. But when I downloaded using PHP and transfer the downloaded file to Mobile and while installing it throws 'there was a problem while parsing the package'.
Here is my PHP code I use to download
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename="' . $file_name . '"');
readfile($file_path);
return true;
fyi...
$file_name is the apk file file 'myfile.apk'
$file_path is the full absolute path of the file in server ('d:\abcd\xyz\xampp\htdocs\apkstore\myfile.apk')
I found one observation while trying to open the APK file using 7-zip.
When I open the file using 7-zip it throws an error 'Cannot open the file xxx as archive'
After I added the below PHP code
header("Content-length: " . filesize($file_path));
Now when I open the file using 7-zip it opens the file but the size of the downloaded file is greater than the original file. And when I open this file in mobile the same error 'there was a problem while parsing the package'
To cut my long story short, I am trying to download an APK file from server to localhost using PHP and I'm able to make it work.

I managed to make it work by adding ob_end_flush()
header('Content-Type: application/vnd.android.package-archive');
header("Content-length: " . filesize($file_path));
header('Content-Disposition: attachment; filename="' . $file_name . '"');
ob_end_flush();
readfile($file_path);
return true;

Related

php open pdf in Windows and Mobile

I am using below php to open pdf, it is working in Windows browser (eg. Windows Google Chrome), but failure to open by Mobile App (eg: Android Google Chrome), it will be download instead of open.
<?php
$com_no = $_POST['comno'];
$date = $_POST["date"];
$name_of_doc = $_POST["name_of_doc"];
// Store the file name into variable
$file = $com_no.'.pdf';
$filename = $date."_".$name_of_doc;
// Header content type
header('Content-type: application/pdf');
header('Content-disposition: inline; filename="'.$filename.'.pdf"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Read the file
#readfile($file);
?>
How shoule I rewrite it to support both open in the Windows and Mobile ?
Thank you very much !
TLDR: you can't program it as it is a browser configuration
If your PDF is opened in your desktop browser you have a configuration in your desktop-browser how to handle files of this type.
By default your browser should ask you what to do with data of that mime type.
You get the option:
open with browser
open with other application
store file
and maybe an option
[ ] always perform this action for files of this type
once you checked the option you no longer get bothered.
BTW:
even if you select open in browser, the file is downloaded in a temp folder to get shown.

Serving .APK file from CDN with headers to install in Android

I want to serve a .APK file to users to download. I have a CDN and it works fine. When I request file download, It downloads from CDN. But I have a problem. My users request downloads from Android devices, in this case, Downloading pure APK file goes trouble because I want to users install that APK file and with pure APK it's not possible as I know. So I create a .php file like this and add 'Content-Type: application/vnd.android.package-archive':
<?php
$file = 'myfile.apk'; //File that we want to send to user.
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($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
When I request download.php, Its work and users can download and install the APK file. And now my question is, In this case, That file downloads from the CDN? I want both download.php and APK file served from CDN because I don't have enough traffic.
Or is this possible to add 'Content-Type: application/vnd.android.package-archive' to downloading file from CDN without php?
PS: When i request pure APK file, Because it's from CDN, It downloads instantly like it's caching, But with download.php, It takes time to download. it means in this case it's not from CDN?
Or is this possible to add 'Content-Type: application/vnd.android.package-archive' to downloading file from CDN without php?
Yes, downloading must work correct in this case.
But with download.php, It takes time to download. it means in this case it's not from CDN?
It takes time, because you use readfile and output buffering. In this case, download started only after php fully load content of target file into memory. This is potential problem, if you plan serve big apk files.
You can serve them, to example in this way:
// set headers here ...
$output = fopen('php://out', 'a');
$target = fopen($target, 'r');
if (!$target || !$output) {
// throw error, if cant read file or
}
// read target file, using buffer size 1024
while (!feof($target)) {
fwrite($output, fread($target, 1024), 1024);
}
fclose($target);
fclose($output);

Force docx file download via PHP corrupt

I know there are a lot of mentions of this but I have tried all the suggestions and nothing seems to work.
I have this script to force download files, but when using docx formats it downloads ok but then says the file is corrupt. However word does manage to open it ok.
Does anyone know why the docx would keep saying there corrupt. I have double checked them by ftp them down from the server and they are fine and open first time.
$documentDir = '/home/';
$file = $_GET['d'];
$fileLocation = $documentDir.$file;
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($fileLocation));
header('Content-disposition: attachment; filename="'.$file.'"');
readfile($fileLocation);
exit(0);
This script works for me.
Are you sure that all your documents are situated in /home/ ?
Don't you mean the relative path home/
If this still doesn't work, can you please tell what's the size of the downloaded docx, and open that file in a text editor like sublime_text, the error will probably be written in there.
You could try modifying disposition line and adding encoding line to ensure encoding is done correctly.
header ("Content-Disposition: attachment; filename*=UTF-8'en'$file");
header('Content-Transfer-Encoding: binary');

Assets graber with zend, big files download as 229 or 228 bytes only

I have the following problem with a server, there many variables at play. So this is what happened. Everything was working perfect when I developed a small webapp with zend on my desktop on fedora. Then I transfer the app to a server on dreamhost and everything worked fine.
So the problem comes with the client that needed a server on china, because they are behind the great firewall and they wanted to transfer their files really faster. They had huge files, around 3.4 GB. so they gave me windows 2003 machine virtual machine, and they couldnt change it to linux, and that is when everything went on a downward spiral.
Basically my app had a folder outside the documentroot, where all the files where going to be upload via ftp. My app read the files and only allowed logged users to download the files.
This is my plugin - controller
<?php
class Mapache_Controller_Plugin_AssetGrabber
extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup (Zend_Controller_Request_Abstract $request)
{
if ($request->getControllerName() != 'assets')
return;
$auth = Zend_Auth::getInstance();
if (!$auth->hasIdentity())
throw new Exception('Not authenticated!');
//$file = APPLICATION_PATH . '/../assets/' . $request->getActionName();
$file = APPLICATION_PATH . '/..' . $_SERVER['REQUEST_URI'];
$file = str_replace("_", " ", $file);
// echo $file;
if (file_exists($file)) {
header("Content-type: ".$this->new_mime_content_type($file));
header('Content-disposition: attachment;');
//header('Content-type: '.$this->new_mime_content_type($file));
//readfile('$file');
echo file_get_contents($file);
}
}
function new_mime_content_type($filename){
$result = new finfo();
if (is_resource($result) === true){
return $result->file($filename, FILEINFO_MIME_TYPE);
}
return false;
}
}
The only thing I changed for it to work on windows, was adding
$file="d:/". $_SERVER['REQUEST_URI'];, so it knows to look on the D: drive of the server.
so basically i have another php file that does an scandir an lists all the files and directories and creates a link to URL/assets/folder/file, it works fine on my test server even with big files. But when I try to download a zip file fo 200 mb from the windows server I get a corrupted zip file of 228 or 229 bytes, like just the header.
The server has xammp with zend installed, I am going crazy.
Migrating back to my dreamhost server will take days, I installed an rsync client on the server and started copying the files but in the last 4 it has only copy 400mb, so my 30 Gb of data will take days.
When i log on to the server with rdesktop and try to download the files I still get 228 bytes of files instead of 200mb or even 3.4Gb.
It has windows 2003.
Do I have to configure something on the apache server, something on the httpd.conf or php.ini?
I found the answer, I need to properly download the file
header('Content-Disposition: attachment;filename="'.basename($file).'"');
header('Content-Description: File Transfer');
//header('Content-Type: application/octet-stream');
header('Content-type: '.$this->new_mime_content_type($file));
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
set_time_limit(0);
$filex = #fopen($file,"rb");
while(!feof($filex))
{
print(#fread($filex, 1024*8));
ob_flush();
flush();
}
//ob_clean();
//flush();
//readfile($file);

"Unable to download. Content not supported" when forcing download on Android device

I'm writing a website which give a APK download automatically when someone accesses the link "http://www.sample-link.com/download/downloadApp
This is my code on PHP site.
$fileName = "./uploads/MyAPK.apk";
header('Content-type: application/download');
header('Content-Disposition: attachment; fileName="' . basename($fileName) . '"');
header('Content-Transfer-Encoding: binary');
readfile($fileName);
When I visit that link on PC, it works. When I do it on Android devices, the download can't be done. It's stuck in the download queue with error "Unable to download. Content not supported". I tried to restart the device but it doesn't work. How to fix?
Use application/vnd.android.package-archive as content type for apks.
Reference: wikipedia

Categories