XML: stream output directly to a user's file

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
icepick
Forum Newbie
Posts: 5
Joined: Fri Mar 13, 2009 9:37 am
Location: Portugal

XML: stream output directly to a user's file

Post by icepick »

Hi!

I am building an aplication that needs to send a XML file to the user. It should be sent as a stream because the file will be kind of big (larger than 20Mb) so it should not be stored in the server's memory before output.

I am using PHP 5.1.6 and the extension XMLWriter.

This extension alows me to do the following:

Code: Select all

 
<?php
   $writer = new XMLWriter(); 
   $writer->openURI('php://output');
   ...
?>
 
The function openURI() determines how the XML should be sent to the user. With the string 'php://output' it is in fact delivered as a stream, without great usage of the server's memory.

Now here's the thing:
Since the stream is delivered trough the browser and browsers are prepared to read the XML, instead of prompting the user with a file save window, the browse simply throws on the screen the content of the XML file that has to be saved.

Naturally, a 'connaisseur' knows what to do: calls for the page's source code and saves it as a XML.
However we all know that most users wont know that much, so I have to find a way to have the file saving window prompted to them. Confronted with 20 or 40 Mb of XML on the screen, they will simply say that the program crashed.

I think the solution probably lies on the use of a different string in the function openURI(), or in the use of some other funtion but I can't find anything about it on the internet. The XMLWriter extension it's kind of recent itself so it is very shortly documented on the official php.net site.

Can anyone give me a clue on this?
User avatar
icepick
Forum Newbie
Posts: 5
Joined: Fri Mar 13, 2009 9:37 am
Location: Portugal

Re: XML: stream output directly to a user's file

Post by icepick »

Solved!

If you want to have the user prompted with a filesave window so that he can save it directly to his disk, you shoul do:

Code: Select all

 
<?php
   header("Content-Type: text/html/force-download");
   header("Content-Disposition: attachment; filename='whatever.xml'");
   $writer = new XMLWriter(); 
   $writer->openURI('php://output');
   ...
?>
 
One down, but still one to go. I have not figured this other one out (yet...)
viewtopic.php?f=14&t=96853
Any of you PHP/XML masters to the rescue of this poor coder ?
SanPuncho
Forum Newbie
Posts: 1
Joined: Tue Apr 14, 2009 3:49 am

Re: XML: stream output directly to a user's file

Post by SanPuncho »

Yeah, you have solved my problem. XML always takes me some troubles. Thanks!
Post Reply