gamini2006
gamini2006

Reputation: 299

How to Force a file to download using PHP on mobile Browsers?

I want to write a php script to download some files(extensions - .apk, .dcm, .pdf, .zip etc...) on mobile browsers. I have written a php code to download those files and it is working fine on all the browsers(not mobile browsers). But I tried it using a HTC mobile and it is trying to open the file instead of downloading(like opening a web page).

How can I enable the download on mobile browsers?

Thank You

PS: This is how I do it. I use a jquery code to send some parameters to a php file and it will return the appropriate download file path.

jQuery code snippets is:

$.post('saveData.php',{ name: name.val(), email: email.val(), phone:phone.val(), address:address.val(), version:vers, swVersion:swvers, type:type }, 
function(data) 
{                       
    var links = data;
    document.body.innerHTML += "<iframe src='" + links + "' style='display:hide;' ></iframe>";
});

"links" variable contains the download path return from the php file. Iframe allow the download window to popup. But it does not work on the mobile browsers.

Upvotes: 3

Views: 8674

Answers (1)

Nikolay Yordanov
Nikolay Yordanov

Reputation: 1404

Try with the following headers:

header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="filename"');

The idea is to set the content type to something that the browser doesn't know how to open - that way it will show the save dialogue. You can try it with the actual MIME type, it should work, but I can't test it right now.

Upvotes: 6

Related Questions