Question

Question: is it possible to make a file download from a web browser to the user's machine without the browser changing its filename?

Background: I have a shareware app and users may buy a license to activate it. The license is an XML file but the extension is ".mlic", not ".xml". mlic is a special file extension my app recognizes. After users pay they are redirected to my PHP script which pushes the mlic file and relevant HTTP headers. My script is below.

Problem: some people are telling me the file downloads as license.xml or license.mlic.xml, and they don't know what to do with this file. I don't know what browser or OS these people are using. I can't replicate. My app is Mac only so this would pertain to OS X.

Solution: can anything be done about this? Are there HTTP headers that explicitly specify a file extension?

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=\"license.mlic\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $filesize($input_path));

ob_end_flush();
@readfile($input_path);
Was it helpful?

Solution

1) Drop useless/undefined header fields, such as Content-Transfer-Encoding and Content-Description.

2) And no, you can't prevent browsers from adjusting the extension based on the content type they detect.

OTHER TIPS

There are http headers that explicitly specify a file extenstion. for that u should set content-Type to for example "application/pdf" for pdf files or "application/ms-excel" for excel files

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top