PHP - Generate file on the fly for android devices - php

I have read some answers here but i cannot seem to make my php script to work.
I generate a text or csv file (it depends what the user has chosen) from a form that it is submitted back to the same page.
The script works fine on Chrome, IE, Mozilla on my desktop but when i try on the stock browser on Android i get an attachment.html file that has the source of my script.
If i try with NEXT browser, i get a file with the proper filename but again inside is the source of my script.
I have read the following link but i cannot make it work
http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
My http headers are the following
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($FileName) . "\"");
header("Content-Length: " . filesize($FileName));
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Connection: close");
print $Content;
exit();
-----Update 1----
I wrote the following code to test things out
<?php
$get = $_GET['get'];
if ($get == 1) {
$content = 'This is a line of text!';
$filename = 'superfile';
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($filename) . ".TXT\"");
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Connection: close");
echo $content;
exit;
}
echo '<html>
<head>
<title>Test</title>
</head>
<body>
<form method="get" action="index.php"
<label>Get</label>
<input type="text" name="get"/>
<input type="submit" name="ok" value="Send"/>
</form>
</body>
</html>';
?>
When i send the form with get method everything works, when i use post i get the html source that you see, same as the problem that i have with my original script.
-----Update 2-----
Okkkkk, now some more info, i think it is related to an Android bug, please check the following link
http://roscopeco.com/2013/04/android-and-the-form-post-download-problem/
Waiting for suggestions to work around this issue, i need method to me post because i pass too many varialbes and i don't want to have on big ugly url on the address bar.

Well, i didn't want to experiment too much so i get the user-agent and i change my post form to a get. Not a great fix but the job is done.

Related

PHP | Download the zip file in php

I want to download the zip file in php.
Here is my code below.
<?php
ob_start();
// set example variables
$filename = "test.zip";
$filepath = "/home/somewhere/file/zip";
// http headers for zip downloads
header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
readfile($filepath.$filename);
?>
And i have a link in my html code.
<html>
<head>
</head>
<body>
Download
</body>
</html>
The file is downloaded successfully when i clicked the link named 'Donwload' but i can't unzip and open the file.
The file name is test.zip when i downloaded for the first time and it is just the file name. Its extension is created when i click it to unzip.
The extension of file is ". cpgz" not the ".zip".
Here is the log when i downloaded it.
Resource interpreted as Document but transferred with MIME type application/zip:"myPHPfile.php"
Did i do something wrong in my code? or miss something?
How can i fix this?
My uploaded file is already zipped in the server and all i want to do is just download it.
The problem is your filepath, there's a missing trailing "/" after "/home/somewhere/file/zip" the working directory should be:
$filepath = "/home/somewhere/file/zip/";
Now this code works great!
<?php
$filepath = iconv("UTF-8","CP949", "/home/somewhere/zip/test.zip");
header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=test.zip");
header("Content-Transfer-incoding: utf-8");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath));
ob_end_flush();
readfile($filepath);
?>

Download A File And Insert Values Into A Database

<?php
// force to download a file
if(isset($_POST['download'])){
$file = "images/dansyo_logo.png";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));
header( "Content-Description: File Transfer");
#readfile($file);
if(#readfile($file)){
echo'proceed';
}else{
echo'failded';
}
}
?>
<form method='post'>
<button name='download'>download</button>
</form>
I have the above code where i want the code to be able to download a file and in the same time insert values into my database.The code is functioning well that is i'm able to download a file.However,nothing is echoed after the file has downloaded or is downloading.
Its not going to echo since header will send the file to browser for download, if you wanted to do any kind of db submission just do it before header sent
$file = "images/dansyo_logo.png";
//Do your DB update here, before the header
if(is_readable($file)) {
// insert into database
} else {
exit(basename($file)." not found.");
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Description: File Transfer");
#readfile($file);
exit();
Note: file_exists() will return true on a directory.

Why does Safari/WebKit appends .html to downloaded files?

I'm having lot of issues with Safari. When I provide a download from a PHP script the downloaded file has ".html" extension appended. Despite the HTTP headers I send to the client, I found them changed in inspector; in particular the mime-type is always set to text/html.
Here what I'm sending to the browser:
<?php
....
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"{$file['name']}\"");
header("Content-Type: ".$file['type']?$file['type']:'application/octet-stream');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file['file']));
ob_end_flush();
#readfile($file['file']);
Where $file contains:
$file = array(
'name'=>'myfile.zip',
'file'=>'/tmp/myfile.zip',
'type'=>'application/zip'
);
What can I do?

In php Instead of downloading csv file it gets open in the browser

I'm trying to download a CSV file through the browser. The script is partially working, because so far I managed to display the CSV on screen, but the download is not starting.
Here is what i tried so far:
if(isset($currency)) {
header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=Pricelogs.csv");
ob_clean();
$filename = "/tmp/".uniqid().".csv";
exportCSVFile($filename, $currency, $country, $provider);
readfile($filename);
//unlink("'".$filename."'");
} else {
echo "ERR_USERNAME_PASSWORD";
exit();
}
I had already read all questions of this type from FAQ & also tried but it gets open on browser only,instead of downloading.
I had also used header with single quotes.
I also tried header("Content-Type: text/csv");
Also:
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
PUT your CSV file URL, it will display in browser in place of force browser to download. You can open it in iframe in your site.
http://docs.google.com/viewer?embedded=true&url=www.yoursite.com/filename.csv
I usually just do:
header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=my_csv_filename.csv");
// print CSV lines here
exit();
I can tell you this combination is working for me:
$bom = chr(0xEF) . chr(0xBB) . chr(0xBF);
header("Content-type: application/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
print $bom . $data;
exit;
If I were you, I would first test this plainly (outside of all your buffering), first see that you manage to make this work, and only then test it in your specific set up.
You might try this.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename.csv\";" );
print $content;
For large files you need to get your output buffer started
add : ob_start(); at the start
ob_clean(); at the end of you file

Get content of a zip file and echo it to user

I wanna make a link for download and all of my file is zip but idont want to my user knows where is my download link
so i wanna get content of zip file and echo it with zip header to user can download it how can i do this?
You should use a "download.php?get=". base64_encode($filename)
When the user click on "download zip", you redirect him with header("Location: download.php?get=". base64_encode($url) to that page.
The headers, here we go
$filename = base64_decode($_GET[get]);
$filepath = "/var/www/domain/httpdocs/download/path/";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
#readfile($filepath.$filename);
Instead of experimenting with headers you could try downloading some 'ready for use' php class. Then your could do something like:
$fileDown = new PHPFileDownload(null, null, 'myFileName', null, 'mypath/myfile.txt');
$fileDown->exportData();

Categories