What is wrong with this code?

I am trying to send compressed data which can be uncompressed at the client side.
I have the control to read the data form the response and format/uncompress at the client side.

<?php 

$data  = '<$pList = $pList ?xml version="1.0" encoding="UTF-8"?>this is testing ';

// I try to compress it into a file
$filename = "test.gz";
$handle = fopen($filename, "w");
$content = gzencode($data  , 9);
//echo $content;
$written = fwrite($handle, $content);
//echo "<br>Written : $written<br>"; 
fclose($handle);


// then i try to uncompress and read it.
$zd = gzopen($filename, "r");
if ($zd) {
	$contents = gzread($zd, 1024);
	if ($contents) {
		gzclose($zd);
//		echo "Contents:", $contents,"<br>";
	} else {
//		echo "Could not read <br>";
	}
} else {
//	echo "Cannot open the file <br>";
}


// Every thing works fine until now.
// But when i echo it to the client. using echo. I can see the encoded format in the browser.

echo gzencode($data  , 9);

?>

but when i use gzread on the client side it does not decompress. i tried to write it to file and decompress and it says the file is corrupted.
I tried adding differed header options to this file but was not successfull.

Any pointers is the solution will be of great help.
Thanks

    Have you tried sending a PHP header setting the content type properly?

    header('Content-Type: application/x-gzip');

      Hello bpat1434,

      Yes i tried it . when i add this . i open the url in the browser an it asks me to dowload the zip file.
      the zip file is named as test.php.gz.
      when i unzip it i get test.php .
      which still has the encoded data. it does not have decoded data. ideally it should have the decoded data since i "unzipped" it.

        Write a Reply...