lintal
lintal

Reputation: 349

PHP generated XML not downloadable in IE

I have an XML feed generated by a PHP script with mod_rewrite running. IE opens the feed OK but I cannot get it to save the file to disk or import to Excel.

The file I am trying to access in the url is similar to:

http://domain.com/download/export.xml

This gets written to a download PHP script and is not actually an XML file.

After setting the headers as below, it is still not possible to get IE to save the file. Also, as it is not capable of displaying the plain source, copy & paste into notepad will not work because of various styling changes IE makes to the XML.

<?php
header("Content-type: text/xml");
header('Content-Disposition: attachment; filename="QuoteExport_'.$quoteDate.'_'.$quoteSlot.'.xml"');

Does anyone know the solution? Thanks.

Edit: Thanks for the help so far. I have tried a combination of these headers and still not getting the results I want. No matter what I set, IE always displays it in-line in the browser with no option to download. The save functionality also still does not work either. Any other ideas how to force IE to save the XML as a file by using headers?

Edit2: The state now is that IE gives the option to open/save but whilst trying to save, there is a popup saying that it is "Unable to open this internet site.". Yet at the same time, the open option displays the content with no issues.

Upvotes: 0

Views: 1429

Answers (4)

lintal
lintal

Reputation: 349

After spending much time changing the headers, I went for a slightly different approach. No matter what I was setting them to, it just would not work for me.

Anyway the solution implemented was: When a client lands on the URL (http~://my-domain.com/downloads/export.xml), the PHP script generates a real XML file in the same location as the request-URI and saves it on the server, schedules it for a delete job in the future and then effectively refreshes the browser. Then, the mod-rewrite rules on the second request serve the actual file and then IE can use the file correctly. A future request then causes the cycle to run again.

Slightly round about way of doing things, but it was the fastest method. Also, I had the ability to schedule file operations from another part of the application making life much easier for clean-up!

Anyway thanks for the help and I did learn a thing-or-two about header options.

Upvotes: 0

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99921

Try setting the content-type to application/xml or application/force-download

Upvotes: 0

Kyle Wild
Kyle Wild

Reputation: 8915

You need to set a session parameter for some versions of IE. Assuming you have $my_file_name and $my_file_contents set, here's how it would look:

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
    session_cache_limiter("public");
}
session_start();

header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename=".' $my_file_name '. "');
print($my_file_contents);

(Adapted from this article)

Upvotes: 2

Yahel
Yahel

Reputation: 8550

To force ie and other browsers to download you have to specify specific headers like in this example :

    header("Content-disposition: attachment; filename=\"".$fileName . ".csv\"");
    header("Content-Type: application/force-download");
    header("Content-Transfer-Encoding: binary");
    header("Pragma: no-cache");
    header("Expires: 0");

Other wise if you use text/xml, ie thinks he knows how to display it and does so.

Upvotes: 5

Related Questions