Outputting PHP files into different file formats

 I'm beginning a list as I've just spent an age trying to get PHP output to create a text file.  Then my client showed me how she then opens the text file in Excel, so I said we could get the script to do that instead.


header('Content-Type: text/plain; charset=utf-8');
header('Content-Disposition: attachment; filename=foo.txt');
echo 'contents of file';
  1.  header('Content-Type: text/plain; charset=utf-8')
  2.  header('Content-Disposition: attachment; filename=foo.txt')
  3.  echo 'contents of file'



$export_file = "my_name.xls"; 
header('Pragma: public'); 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); 
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT'); 
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header ("Pragma: no-cache"); 
header("Expires: 0"); 
header('Content-Transfer-Encoding: none'); 
header('Content-Type: application/vnd.ms-excel;'); 
header("Content-type: application/x-msexcel");  
header('Content-Disposition: attachment; filename="'.basename($export_file).'"');
  1.  $export_file = "my_name.xls"; 
  2.  ob_end_clean()
  3.  ini_set('zlib.output_compression','Off')
  4.  header('Pragma: public')
  5.  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT")
  6.  header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT')
  7.  header('Cache-Control: no-store, no-cache, must-revalidate')
  8.  header('Cache-Control: pre-check=0, post-check=0, max-age=0')
  9.  header ("Pragma: no-cache")
  10.  header("Expires: 0")
  11.  header('Content-Transfer-Encoding: none')
  12.  header('Content-Type: application/vnd.ms-excel;')
  13.  header("Content-type: application/x-msexcel")
  14.  header('Content-Disposition: attachment; filename="'.basename($export_file).'"')
Category: Personal Home Page :: Article: 240

Credit where Credit is Due:

Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman

Related Articles

Joes Revolver Map


Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Donate to Joel Lipman via PayPal

Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 bc1qjtp4l4ra452wzvuk9a45yfj82zkahsyy2z379y
© 2022 Joel Lipman .com. All Rights Reserved.