Force image download with php using header() in smartphones and tablets - php

I was searching already for a long time and I havent seen any right answer yet.
I'm trying to create a system in PHP where the user can download a signPicture that I create in JPG.
The program is working fine in all desktop computers. There is not problem at all, even for IE8.
The header that I use:
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="test.jpg"');
in the end i just stream the picture:
imagejpeg($imgSign,NULL,100);
How I said, it's working really good in every browser. But then we get to the mobile devices, where in android for example, download a test.jpg file... but then it cannot open... and the same with ipad (actually doesnt download, it show the image in the browser and than I save it... but it does not open either).
I also try more examples that I saw, but doesnt change anything, like:
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-Type: application/download");
header("Content-Transfer-Encoding: binary ");
Any idea how to sort this out in mobile devices?
Thanks!

I got it!
There were differents problems. I found the clear solution in comments from this post:
http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="test.JPG"');
The important steps: I send everything with a form. The form, to make it work in mobiles, needs to have the target='_top' and the method='get'
It also make errors if the extention (jpg) is not in UPPERCASE and the file name is not between " ".
Now it works in all devices that I try by far. :)
Special thanks to Jörg Wagner, author of the post.

Related

Why is my PDF download corrupted?

I have a script that generates a pdf using fpdf, this file is saved correctly on the server on my computer. In that same php file I run the following code to download the file. When i download the pdf and I check it in notepad, everything in the pdf is the same, except for the fact that it contains a lot of my previous files html at the beginning of it. The file saved to my server doesn't have any of that.
What could cause something like this to happen? I have no idea where to look for the source of this error, can anyone point me in the direction to finding the problem?
<?php
$filename=($name.$ran.'.pdf');
$pdf->Output($name.$ran.'.pdf');
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octetstream');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($filename));
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
readfile($name.$ran.'.pdf');
?>
I managed to figure things out, thanks to hakre! Essentially I just needed to clear out the output buffer using ob_clean () before using readfile() and the code worked!

Headers for force download of apk file

I'm doing a script that increases the counter for an APK file's download then sends the file to the browser for download.
Here's what I have:
<?php
$file = "android.apk";
function force_download($file){
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
die(file_get_contents($file));
}
force_download($file
The problem is that with a browser like firefox, it downloads but it is like 'android.apk - 0 bytes'. So it essentially, it does not download the file's contents.
What might I be doing wrong? A solution for this?
IMPORTANT: It has to work on mobile.
);
I've never accessed a .apk link that didn't force a download, so I'm not sure what the need for a force download is on that. As far as incrementing the counter, I would probably just link to a page that forwards to the apk file after the counter has been done.
For instance link someone to: getapk.php?apkid=1
Then on getapk.php do something like this:
$update = mysql_query("UPDATE apps SET downloads...");
if ( $update ) { header("Location: appname.apk"); }
Of course that leaves out a lot of details, but if you need help with anything else I'd be happy to provide more details.
I've realized that I don't need to use complex header info expecially if the script will be moved from server to server where the .apk mime type is not native and may therefore be hard for a novice to set up.
A simple redirect will do:
$file_name = $_GET['f']; //$_GET['f'] has the link to the file like http://mydomain.com/file/android.apk
//Do database query or increase download counter
header('location: '.$file_name);
Voila! I have increased the counter and the download will be pushed to the browser.

Save Export CSV to desired location - PHP

I have a code like this.
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");
Now, this gets saved in my desktop. If i wish to save the xls file in D:/xls/
then how to add the code.
Please help me.
Thanks -
Haan
If i wish to save the xls file in D:/xls/ then how to add the code.
The server can not dictate to the client where to save a file.
That is a decision only the client can make.
Imagine if the server could tell the client where to save a file. You could overwrite any file on the user's system. That'd be the biggest security breach in the history of poorly designed internet security breaches.

PHP_excel: Export to xlsx gets downloaded successfully but when we open it it shows warning Excel found unreadable content

i am using php_excel to export to xlsx. In my application im making use of template.
When i download, the xlsx file gets downloaded fine, but when we open its showing the following warning:
"Excel found unreadable content in 'project_report(3).xlsx'. Do you want to recover the contente of this workbook? If you trust the source of this workbook, click Yes."
If i click yes it opens the file correctly.
And one more thing is when i attach the downloaded file to the mail. And if i open it sing Google Spread sheet it says bad format unable to open.
so if anyone know the reason please suggest me to solve this.
check your headers, here are mine:
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-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
$objWriter->save('php://output');
check for output (spaces before <?php forgotten echo? etc

Problems with header() when displaying a PDF file in IE8

So, I have a file that sends the following:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: private");
header("Content-type: application/pdf");
header("Content-disposition: inline; filename=file.pdf");
header("Content-length: 7735");
then I echo out the file - it is a PDF file.
Works fine in IE6 & 7 on XP (and FF for that matter)
The very same code shows nothing when running on IE8 on either XP or Vista.
There are no security warnings, etc so I don't think it has to do with that.
And, if my memory serves me correctly, this worked on IE8 a while ago.
What am I doing wrong here? Am I missing something out of the headers?
Is there a way for me to see what header information normal comes over when viewing a PDF in IE8 so I know what to emulate?
After looking at things it still works in IE8 EXCEPT when SSL is on
Under HTTPS and IE8, those headers fix the download problem:
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
Other X-something headers did not make any difference.
It has probably to do do with the SSL. I read this article (in German, with code examples) where the author set the following header:
header('Pragma: anytextexeptno-cache', true);
I'm not sure what is needed, but here is what you could do.
Put the file temporarily in a public place on your server, make syre you can download that with a direct link in IE8, Use firefox LiveHTTP headers or similar to grab all headers that the server sends. Spit them out in exactly the same way and order in your script. (And don't forget to delete the file).
Something I want to add, as I faced this problem, too, in a slightly different way using Joomla.
Normal PDF-Output of content worked fine, in all browsers.
But the generation of a pdf from within my own component (using JDocument, tho) generated the bevahiour mentioned above.
My solution: Explicitly enable caching for my component using the following statement in view.html.php:
JResponse::allowCache(true);
Maybe that helps somebody.
I'm using HTTPS and i had some problems, but using those headers the download did.
Try it.
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header("X-Download-Options: noopen "); // For IE8
header("X-Content-Type-Options: nosniff"); // For IE8
header("Content-type: application/pdf");
header("Content-disposition: inline; filename=file.pdf");
header("Content-length: 7735");
The problem is you cant direct open. Just save.
Possibly related: Can't display PDF from HTTPS in IE 8 (on 64-bit Vista)

Categories