简体   繁体   English

如何使用PHP提供gzip xml文件供下载

[英]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. 我有一个PHP脚本,可以从db创建一个xml文件。 I would like to have the script create the xml data and immediately make a .gzip file available for download. 我想让脚本创建xml数据并立即生成.gzip文件供下载。 Can you please give me some ideas? 你能给我一些想法吗?

Thanks! 谢谢!

You can easily apply gzip compression with the gzencode function. 您可以使用gzencode函数轻松应用gzip压缩。

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

echo gzencode($xmlToCompress);

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
?>

If you don't have the gzip module available, 如果您没有可用的gzip模块

<?php

  system('gzip filename.xml');

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM