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

1

3

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.

Layamon answered 18/9, 2011 at 12:39 Comment(3)
are you sending the correct mime type headers ??Hindquarter
This is way too little information. Please show some codeWavellite
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.Layamon
D
6

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.

Denticulate answered 18/9, 2011 at 13:31 Comment(2)
upvoted but would suggest you make it a suggestion rather than a questionReniti
This still plays MP3s in the browser on an iPhone. I wish there were a way to allow downloads but prevent streaming.Resume

© 2022 - 2024 — McMap. All rights reserved.