Page 1 of 1

Forcing an XML file download ... where to start?

Posted: Fri Jul 18, 2008 10:22 am
by jeffrydell
Sorry if I'm not using the proper terminology here, but it's the first time I'm trying to do this and I'm not sure what keywords I should be looking for.

I have a script that generates a MySQL query on demand and stores the result set in an XML file on my web server. I would like to then offer that file to the site visitor as a download - but want to be sure it gets 'pushed' to the browser as a download (have the dialog box pop up asking where to save the file), not to have the browser open and read the file.

Ultimately my clients take this data and import it to an Access or FileMaker Pro file on their PC.

Is this a php thing, an html thing, do I HAVE to use Javascript? My absolute preference would be to do this as much in php as possible.

Thanks for whatever help or suggestion or link you can offer!

Jeff

Re: Forcing an XML file download ... where to start?

Posted: Fri Jul 18, 2008 11:01 am
by jaoudestudios
Here is a php function that will force the browser to download the file.
http://www.forum.jaoudestudios.com/view ... p?f=13&t=8

Re: Forcing an XML file download ... where to start?

Posted: Fri Jul 18, 2008 11:21 am
by jeffrydell
So, if I'm reading this correctly, I could pass my XML output right to this function as a big fat variable rather than writing it to a file on the web server.

... AND I wouldn't have to pass the file size because your function will calculate it?

If so, this is REALLY slick!

Jeff

Re: Forcing an XML file download ... where to start?

Posted: Fri Jul 18, 2008 11:33 am
by alex.barylski
I think all you need to do is set the right headers and echo the data -- no need to actually save it to disk first:

From php.net

Example #1 Download dialog

If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.

Code: Select all

 
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
 
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
 
// The PDF source is in original.pdf
readfile('original.pdf');
?>

Re: Forcing an XML file download ... where to start?

Posted: Fri Jul 18, 2008 7:54 pm
by RobertGonzalez
Hockey nailed it. Send appropriate response headers (before any output so we don't have to see another post about "Headers already sent" ;) ) then print() the file.

Re: Forcing an XML file download ... where to start?

Posted: Fri Jul 18, 2008 9:39 pm
by jeffrydell
Thanks all!

In my case, no worries about the header() issue - I've already experienced (and learned from) all those mistakes.

Jeff

Re: Forcing an XML file download ... where to start?

Posted: Sat Jul 19, 2008 12:02 am
by RobertGonzalez
Hey, any chance I get to make a preemptive strike, I take. :wink:

Glad you got it all sorted out.