How to gzip a file remotely over a website...

Consti used Ask the Experts™
My question is how to gzip files on a server, remotely through a script like:
function create_gzip_file($src_file, $dest_file)
$data = implode("", file($src_file));
$gz_data = gzencode($data);

$fp = fopen($dest_file, "w+");
fwrite($fp, $gz_data);

create_gzip_file("text.txt", "test.txt.gz");
But this one doesnt work for my server (
Please give me any advises. All files created through this script have only 10-50KB size and the file in the *.gz has 0 byte size.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Do you have zlib compiled into php?

Here are the notes from the documentation

Zlib support in PHP is not enabled by default. You will need to configure PHP --with-zlib[=DIR]

The windows version of PHP has built in support for this extension. You do not need to load any additional extension in order to use these functions.

Note: Builtin support for zlib is available with PHP 4.3.0.


The problem is, the server is not my server, it is just some webspace I bought. They say they have PHP installed, version 4.3.0 (or higher) I guess. My phpBB (forum) is able to gzip the mysql database etc. so I think there is a way to gzip files over a php script.
Would be nice if you could help me out, because I seriously need the help :-)
Thanks for posting,

Is there a way I can figure out if zlib is compiled into php? I am able to telnet my server with my username, gzip, php and perl are only some of the available commands. Thanks for helping me out

You need to create a php file and put in it:




then access it through your browser.

If you have 4.3.0 installed, it will be apparent by the enormous 'PHP Version x.x.x' text across the top of the page.  

After that, check the 'Configure Command' section, if it contains '--with-zlib' then you have it zlib installed and your problem isn't there.

I host my personal websites through a commercial provider, and they rarely have these kind of options compiled in.

PS, delete this file after you have used it, having phpinfo information visible can sometimes be a security problem.  
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.


Here is my output:

PHP Version 4.1.2

So it seems as it is installed, and configured so the problem has to lie somewhere else, if u want me to do so, I can send u the whole report of my phpinfo file.
Thank you for your fast answer,
Hmm, no, thats fine for now.

Your problem may then lie with permissions.  Check that your webserver has rwx permission for the directory and file you are trying to write this file too.  

Try creating a file and writing to it with methods other than the gz ones, (like fopen).

I've had some funny results with permission problems in PHP.

I'll come clean and say I haven't actually used the gzip stuff - so these are just general trouble shooting hints.

Good luck!


Thanks, seems as I figured out what the problem was :-)
The script was completely working, but I (as a dumb german) didnt use the right script.. I had another one, but it didn't work :-)
Anyway, thanks, you get the points. The script I posted is working, and the directory only needs chmod 755.
Thank you for your patience,

I've done that more than once myself.  No worries.

Thanks for the points!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial