pistolshrimp
pistolshrimp

Reputation: 1039

How can I offer a gzipped xml file for download using PHP

I have a PHP script that creates an xml file from the db. I would like to have the script create the xml data and immediately make a .gzip file available for download. Can you please give me some ideas?

Thanks!

Upvotes: 3

Views: 5450

Answers (3)

Pim Jager
Pim Jager

Reputation: 32129

Something like this?

<?php
 header('Content-type: application/x-gzip');
 header('Content-Disposition: attachment; filename="downloaded.gz"');
 $data = implode("", file('somefile.xml'));
 print( gzencode($data, 9) ); //9 is the level of compression
?>

Upvotes: 2

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827536

You can easily apply gzip compression with the gzencode function.

<?
header("Content-Disposition: attachment; filename=yourFile.xml.gz");
header("Content-type: application/x-gzip");

echo gzencode($xmlToCompress);

Upvotes: 13

kkyy
kkyy

Reputation: 12460

If you don't have the gzip module available,

<?php

  system('gzip filename.xml');

?>

Upvotes: 0

Related Questions