kamal
kamal

Reputation: 237

how to compress a sitemap with php

I have this below code and it work fine

        header ("content-type: text/xml");
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
        $xml .= '<urlset xmlns="http://www.google.com/schemas/sitepam/0.84">';
        $xml .= '<url><loc>'.SiteRoot.'</loc><changefreq>daily</changefreq><priority>1.0</priority></url>';
        $xml .= '<url><loc>'.SiteRoot.'/directory</loc><changefreq>daily</changefreq><priority>0.9</priority></url>';
        $Query = mysql_query ("SELECT link FROM `om` ORDER BY `link`");
        while($row = mysql_fetch_array($Query)) {
            $xml .= '<url>';
            $xml .= '<loc>'.GenerateLink( 'link',$row['link'] ).'</loc>';
            $xml .= '<changefreq>weekly</changefreq>';
            $xml .= '<priority>0.8</priority>';
            $xml .= '</url>';
        }
        $xml .= '</urlset>';
        echo $xml;

When i try to compress it with mime header

header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="sitemap.xml.gz"');

Browser download a .gz file but it's not open. winrar give me a error that said: The archive is either in unknown format or damaged

This is the final code :

    //  header ("content-type: text/xml");
        header('content-type: application/x-gzip');
        header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
        $xml .= '<urlset xmlns="http://www.google.com/schemas/sitepam/0.84">';
        $xml .= '<url><loc>'.SiteRoot.'</loc><changefreq>daily</changefreq><priority>1.0</priority></url>';
        $xml .= '<url><loc>'.SiteRoot.'/directory</loc><changefreq>daily</changefreq><priority>0.9</priority></url>';
        $Query = mysql_query ("SELECT link FROM `om` ORDER BY `link`");
        while($row = mysql_fetch_array($Query)) {
            $xml .= '<url>';
            $xml .= '<loc>'.GenerateLink( 'link',$row['link'] ).'</loc>';
            $xml .= '<changefreq>weekly</changefreq>';
            $xml .= '<priority>0.8</priority>';
            $xml .= '</url>';
        }
        $xml .= '</urlset>';
        echo $xml;

Upvotes: 2

Views: 4572

Answers (1)

steveo225
steveo225

Reputation: 11892

Try using some of the built in gzip functions like gzencode

echo gzencode($xml);

Upvotes: 10

Related Questions