GZ Compressing an XML sitemap file with PHP
I have a PHP script that generates a dynamic PHP sitemap from my site's database to an xml file开发者_Python百科 using fopen()
and fwrite()
.
How can I compress this file using GZ compression dynamically as I write it?
I tried fwrite()
-ing strings that I ran through gzcompress()
into the file and renaming it ".xml.gz", but it doesn't seem the file it;s creating is a well-fromed archive.
Not using fopen and fwrite but gzopen()
and gzwrite()
should do the trick for you.
From the manual:
# Sample #1 gzwrite() example
<?php
$string = 'Some information to compress';
$gz = gzopen('somefile.gz','w9');
gzwrite($gz, $string);
gzclose($gz);
?>
If i understood correctly
this is a quote from the php site that we all should keep in mind.
Take Heed 07-Nov-2010 08:50 Read the description of gzwrite() very carefully. If the 'length' option is not specified, then the input data will have slashes stripped on systems where magic quotes are enabled. This is important information to know when compressing files.
http://php.net/manual/en/function.gzwrite.php
精彩评论