PHP Android how to dowload apk file - php

I have Android app on my server and also have php code like:
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename="test.apk"');
readfile('test.apk');
My mobile with default web browser is downloading this file in the way of reading apk file on the screen. I expected some kind of dialogue like: save it as or/and where do you want to save it instead.
What I am doing wrong?
Is there any way to save it automatically, meaning without any dialogue?

Try:
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-Type: application/vnd.android.package-archive');
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="test.apk"');
readfile('C4A.apk');

you should try one of these:
check the mime type configuration on your server. APK Files
add the apk extension to apache
also, check this question. However the server is .net, not php.

Related

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

Download file from server php using headers

I have xampp web server, and trying to download file using headers! Don't know what is wrong, but file not starting to download and not appears in browser! In http response I have the source of file!
header("Content-disposition: attachment;filename=d:\\archive\\result.csv");
header("Content-type: application/pdf");
readfile("sample.pdf");
Can any one halp me, please!
Try this
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=file:///D:\archive\result.csv' );

I have php url issue for read pdf

Now I am developing codeigniter site.
I have one issue.
In order to read&open pdf into web browser.
header("Content-type:application/pdf");
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Cache-Control: max-age=0'); //no cache
header("Content-Disposition:attachment;filename=\"".$file."\"");
readfile($filePath);
above code $file is filename and $filePath is pdf file path.
When it runs on local server, $filePath is value such as "http://localhost/.pdf" and it runs well.
But when runs on hosting server, this value is "http://.com/***.pdf"
And doesn't run.
We can not open with pdf format error.
file content didn't include readed.
I know that is cause of URL issue.
But I have no issue!
Your Content-Disposition should be inline if you want to display the file in the browser and not as a download but I guess it doesn't really matter if you can get it to work.
If your allow_url_fopen config in PHP is set to Off, you will not be able to read URL file from within your PHP script.
Anyway, your code should look something like this.
<?php
$file = "lesson2.pdf";
$filePath = "http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf";
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Content-Disposition: inline; filename=\"" . $file . "\"");
readfile($filePath);

Firefox downloads(tries to open) .zip file as .HTM

I am creating downloadable zip file, it works fine almost everywhere. But in Mozilla Firefox on save of this zip I get strange message that my_zip.zip is HTM file (sorry for the language, but I hope it is pretty understandable):
If I choose save option it will be saved as normal zip (no sign of HTM at all), but in "open as" section there are only programms for opening HTM
So, the question is How to make Firefox detect this zip as zip?
I am currently using this headers (set by PHP):
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$zipFileName");
header("Content-Transfer-Encoding: binary");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store");
header('Pragma: no-cache');
header("Content-length: " . filesize($zipFileName));
readfile($zipFileName);
Already tried using header("Content-Type: application/zip"); , does not work; plus application/zip is not standart (as I read here in some headers related question).
I am using Mozilla Firefox v40.0.3, the php project is using Laravel 5.1 (I doubt it has anything to do with this)
UPDATE:
While trying different application\[format]s , I added a dump and die command after headers
//bunch of kosher headers here...
readfile($zipFileName);
dd(headers_list());//dumps and dies
And I get a zip type in download window. Then I figured out that after die or exit I will always get right download type of zip; Then I deleted all dump-and-die sections , but download type remains as zip. I have no idea what i have fixed by this manipulations.
I would love to have an explanation of this strange situation
A quick google search suggests the Content-Type seems to be the culprit
header("Content-Type: application/octet-stream");
Try setting it to application/x-zip-compressed ?
ALso the comments in this bug report may be useful: https://bugzilla.mozilla.org/show_bug.cgi?id=540900

Create XML, Json or Text file without saving on the server with PHP [duplicate]

I have a CSV file on my server. If a user clicks on a link it should download, but instead it opens up in my browser window.
My code looks as follows
<a href="files/csv/example/example.csv">
Click here to download an example of the "CSV" file
</a>
It's a normal webserver where I have all of my development work on.
I tried something like:
<a href="files/csv/example/csv.php">
Click here to download an example of the "CSV" file
</a>
Now the contents of my csv.php file:
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
Now my issue is it's downloading, but not my CSV file. It creates a new file.
.htaccess Solution
To brute force all CSV files on your server to download, add in your .htaccess file:
AddType application/octet-stream csv
PHP Solution
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
readfile("/path/to/yourfile.csv");
Or you can do this using HTML5. Simply with
<a href="example.csv" download>download not open it</a>
This cannot be done reliably, since it's up to the browser to decide what to do with an URL it's been asked to retrieve.
You can suggest to the browser that it should offer to "save to disk" right away by sending a Content-disposition header:
header("Content-disposition: attachment");
I'm not sure how well this is supported by various browsers. The alternative is to send a Content-type of application/octet-stream, but that is a hack (you're basically telling the browser "I'm not telling you what kind of file this is" and depending on the fact that most browsers will then offer a download dialog) and allegedly causes problems with Internet Explorer.
Read more about this in the Web Authoring FAQ.
Edit You've already switched to a PHP file to deliver the data - which is necessary to set the Content-disposition header (unless there are some arcane Apache settings that can also do this). Now all that's left to do is for that PHP file to read the contents of the CSV file and print them - the filename=example.csv in the header only suggests to the client browser what name to use for the file, it does not actually fetch the data from the file on the server.
Here is a more browser-safe solution:
$fp = #fopen($yourfile, 'rb');
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="yourname.file"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize($yourfile));
}
else
{
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="yourname.file"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize($yourfile));
}
fpassthru($fp);
fclose($fp);
Configure your server to send the file with the media type application/octet-stream.
This means that your browser can handle this file type.
If you don't like it, the easiest method would be offering ZIP files. Everyone can handle ZIP files, and they are downloadable by default.
Nice clean solution:
<?php
header('Content-Type: application/download');
header('Content-Disposition: attachment; filename="example.csv"');
header("Content-Length: " . filesize("example.csv"));
$fp = fopen("example.csv", "r");
fpassthru($fp);
fclose($fp);
?>
A previous answer on this page describes how to use .htaccess to force all files of a certain type to download. However, the solution does not work with all file types across all browsers. This is a more reliable way:
<FilesMatch "\.(?i:csv)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
You might need to flush your browser cache to see this working correctly.
If you are doing it with your application itself... I hope this code helps.
HTML
In href -- you have to add download_file.php along with your URL:
<a class="download" href="'/download_file.php?fileSource='+http://www.google.com/logo_small.png" target="_blank" title="YourTitle">
PHP
/* Here is the Download.php file to force download stuff */
<?php
$fullPath = $_GET['fileSource'];
if($fullPath) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-Disposition: attachment; filename=\"" . $path_parts["basename"]."\""); // Use 'attachment' to force a download
header("Content-type: application/pdf"); // Add here more headers for diff. extensions
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"" . $path_parts["basename"]."\"");
}
if($fsize) { // Checking if file size exist
header("Content-length: $fsize");
}
readfile($fullPath);
exit;
}
?>
To force download you may use Content-Type: application/octet-stream header, which is supported by most browsers:
function downloadFile($filePath)
{
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
}
A BETTER WAY
Downloading files this way is not the best idea especially for large files. PHP will require extra CPU / Memory to read and output file contents and when dealing with large files may reach time / memory limits.
A better way would be to use PHP to authenticate and grant access to a file, and actual file serving should be delegated to a web server using X-SENDFILE method (requires some web server configuration):
X-SENDFILE is natively supported by Lighttpd: https://redmine.lighttpd.net/projects/1/wiki/X-LIGHTTPD-send-file
Apache requires mod_xsendfile module: https://tn123.org/mod_xsendfile/ On Ubuntu may be installed by: apt install libapache2-mod-xsendfile
Nginx has a similar X-Accel-Redirect header: https://www.nginx.com/resources/wiki/start/topics/examples/xsendfile/
After configuring web server to handle X-SENDFILE, just replace readfile($filePath) with header('X-SENDFILE: ' . $filePath) and web server will take care of file serving, which will require less resources than using PHP readfile.
(For Nginx use X-Accel-Redirect header instead of X-SENDFILE)
Note: If you end up downloading empty files, it means you didn't configure your web server to handle X-SENDFILE header. Check the links above to see how to correctly configure your web server.

Categories