4

I am creating an xml file on the fly. When a user generates this file I want it to open up a download file dialog with the content that was generated. There is no actual file because it is just generated through php. Any ideas on how to do this?

ngreenwood6
  • 8,108
  • 11
  • 33
  • 52

3 Answers3

14

This is what worked for me. In readfile('newfile.xml'); make sure to give the path of the file correctly. This php page is called from an html page with anchor tag which says - download:

<?php 
header('Content-disposition: attachment; filename="newfile.xml"');
header('Content-type: "text/xml"; charset="utf8"');
readfile('newfile.xml');
?>

source: How do I force the browser to download a file to disk instead of playing or displaying it?

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Hari Gudigundla
  • 812
  • 10
  • 20
8

Send a content-disposition attachment header.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • You obviously didnt read my post. There is no actual file to read. It is just sent to the page and I do not want to create a file just be able to download what it generates. – ngreenwood6 Apr 23 '10 at 06:00
  • 2
    @ngreen you obviously didn't read the linked article. set the said header and echo out the xml data. – Amarghosh Apr 23 '10 at 06:06
  • oh nice I thought you actually had to assign it that file...thanks for the info – ngreenwood6 Apr 23 '10 at 06:14
  • This link sends to a Loan company, nothing to do with the question. No idea how it has been redirected on that... – No_or_yes Jan 21 '20 at 10:45
  • @No_or_yes — Domains expire and get bought by other people. I've replaced the link with one to an archive. – Quentin Jan 21 '20 at 10:48
0
header('Content-disposition: attachment; filename=advertise.xml');
header ("Content-Type:text/xml"); 
//output the XML data
echo  $xml;
 // if you want to directly download then set expires time
header("Expires: 0");
Milind Morey
  • 2,100
  • 19
  • 15