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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeffrydell
Forum Commoner
Posts: 77
Joined: Thu Jan 17, 2008 4:39 pm
Location: Menasha, WI

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

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

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

Post 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
jeffrydell
Forum Commoner
Posts: 77
Joined: Thu Jan 17, 2008 4:39 pm
Location: Menasha, WI

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

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

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

Post 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');
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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

Post 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.
jeffrydell
Forum Commoner
Posts: 77
Joined: Thu Jan 17, 2008 4:39 pm
Location: Menasha, WI

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

Post by jeffrydell »

Thanks all!

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

Jeff
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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

Post by RobertGonzalez »

Hey, any chance I get to make a preemptive strike, I take. :wink:

Glad you got it all sorted out.
Post Reply