views:

849

answers:

3

hi there!

i have written a script that prints a XML file to the screen but I want it to open a download dialog so that i could save it as a file.

how could i do that?

thnx!

the script:

<?php
print '<?xml version="1.0" encoding="UTF-8" ?>';
print "\n <data>";
...
print "\n </data>";
?>
+2  A: 

try setting the headers right:

<?php
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="example.xml"'); 
header('Content-Transfer-Encoding: binary');

print '<?xml version="1.0" encoding="UTF-8" ?>';
print "\n <data>";
...
print "\n </data>";
?>
stefita
this is my favourite but doesn't work..-(
headkit
You might try this again putting an `ob_end_clean();` call in front of the header declarations. From your tags I assume this happens in a Drupal context, and Drupal might interfere by already having set some headers upfront. Clearing the output buffer might make a difference.
Henrik Opel
Do you get any warnings that the header were already sent? Look in your apache log in /var/log/apache2/ or so. In that case you can try Henrik's suggestion.
stefita
+1  A: 

Try using the following to force the browser to show the "Save As..." dialog: The browser shows a "Save As..." dialog for content types that it doesn't know how to interpret / display, or when it is instructed to in the headers. Just know the correct headers and you can specify to download it, the default filename, content type and how it should be cached.

<?php
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= "\n <data>";

// Create the rest of your XML Data...

$xml .= "\n </data>";
downloader($xml, 'yourFile.xml', 'application/xml');

The function code:

<?php
if(!function_exists('downloader'))
 {
  function downloader($data, $filename = true, $content = 'application/x-octet-stream')
   {
    // If headers have already been sent, there is no point for this function.
    if(headers_sent()) return false;
    // If $filename is set to true (or left as default), treat $data as a filepath.
    if($filename === true)
     {
      if(!file_exists($data)) return false;
      $data = file_get_contents($data);
     }
    if(strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== false)
     {
      header('Content-Disposition: attachment; filename="'.$filename.'"');
      header('Expires: 0');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Content-Transfer-Encoding: binary');
      header('Content-Type: '.$content);
      header('Pragma: public');
      header('Content-Length: '.strlen($data));
     }
    else
     {
      header('Content-Disposition: attachment; filename="'.$filename.'"');
      header('Content-Transfer-Encoding: binary');
      header('Content-Type: '.$content);
      header('Expires: 0');
      header('Pragma: no-cache');
      header('Content-Length: '.strlen($data));
     }
    // Send file to browser, and terminate script to prevent corruption of data.
    exit($data);
   }
 }
mynameiszanders
A: 

thank you all for your help!

headkit