This worked unstable for me under high load (50+ files per second):
<?php
$gz = gzopen ( $file, 'w9' );
gzwrite ( $gz, $content );
gzclose ( $gz );
?>
The following works fine:
<?php
$f = fopen ( $file, 'w' );
fwrite ( $f, gzcompress ( $content, 9 ) );
fclose ( $f );
?>
gzopen
(PHP 4, PHP 5)
gzopen — Abre un archivo gz
Descripción
Abre un archivo gzip (.gz) para lectura o escritura.
gzopen() se puede usar para leer un archivo el cual no esté en formato gzip; en este caso gzread() leerá directamente el archivo sin descomprimirlo.
Parámetros
- filename
-
El nombre del archivo.
- mode
-
Como en fopen() (rb o wb) pero también puede incluir un nivel de compresión (wb9) u una estrategia: f para datos filtrados como en wb6f, h para compresión Huffman solamente como en wb1h. (Ver la descripción de deflateInit2 en zlib.h para más información sobre el parámetro de estrategia.)
- use_include_path
-
Se puede configurar este parámetro opcional en 1, si se desea buscar también el archivo en la ruta include_path.
Valores devueltos
Retorna un apuntador hacia el archivo abierto, después de eso, cualquier cosa que se lea desde este descriptor de archivo sera descomprimido de forma transparente y lo que se escriba será comprimido.
Si falla la apertura, la función retorna FALSE.
Ejemplos
Example #1 Ejemplo de gzopen()
<?php
$fp = gzopen("/tmp/file.gz", "r");
?>
WARNING gzopen and gzread have a major disadvantage. They makes NO checksum and NO length verification of the gzipped data and discard this valuable information. This should be documented here.
dtorop932 at hotmail dot com's comments, according to my tests, is incorrect. That code wishes to download the entire file before parsing, which is inconvinient. The wget method works though.
Be aware that when opening a remote file on a http server the gzopen will return by default false after 120 seconds waiting to any answer.
RE dubious's comment: "Being able to read gzip streams from ftp and http is near the top of my personal wishlist at the moment..."
One way to read a gzip stream over http is to daisychain stream wrappers, e.g.:
<?
$fp = fopen("compress.zlib://http://some.website.org/example.gz", "r");
?>
"On the fly" gunzipping actually DOES seem to work - it just appears that only LOCAL streams/files (including php://stdin) can be accessed for some reason. I THINK (but have not yet tested) that you could similarly gzopen "php://stdout" and pass a stream of gzipped data to the browser (when run from a web page) or console (when run standalone) through there.
I HAVE tested scripts from the command line like:
wget -q -O- ftp://some.host.net/pub/some_gzip_file.gz | php gunzip_stuff.php
where gunzip_stuff.php would be a script that gzopened "php://stdin" and did gzgets from that stream, and it seems to work fine, but that obviously doesn't help someone wanting to grab gzipped streams from remote sites from a web-based script.
Being able to read gzip streams from ftp and http is near the top of my personal wishlist at the moment...
