The zlib extension contains versions of many file-access functions, such as fopen(), fread(), and fwrite() (called gzopen(), gzread(), gzwrite(), etc.) that transparently compress data when writing and uncompress data when reading. The compression algorithm that zlib uses is compatible with the gzip and gunzip utilities.
Use PHP's zlib extension to read or write gzip'ed files. To read a compressed file:
$zh = gzopen('file.gz','r') or die("can't open: $php_errormsg");
while ($line = gzgets($zh,1024)) {
// $line is the next line of uncompressed data, up to 1024 bytes
}
gzclose($zh) or die("can't close: $php_errormsg");
Here's how to write a compressed file:
$zh = gzopen('file.gz','w') or die("can't open: $php_errormsg");
if (-1 == gzwrite($zh,$s)){ die("can't write: $php_errormsg"); }
gzclose($zh)or die("can't close: $php_errormsg");
In addition, other functions are, gzgets($zp,1024) that works like fgets($fh,1024). It reads up to 1023 bytes, stopping earlier if it reaches EOF or a newline. For gzgets( ), this means 1023 uncompressed bytes.
However, gzseek() works differently than fseek(). It only supports seeking a specified number of bytes from the beginning of the file stream (the SEEK_SET argument to fseek( )). Seeking forward (from the current position) is only supported in files opened for writing (the file is padded with a sequence of compressed zeroes). Seeking backwards is supported in files opened for reading, but it is very slow.
The zlib extension also has some functions to create compressed strings. The function gzencode( )compresses a string and gives it the correct headers and formatting to be compatible with gunzip. Here's a simple gzip program:
$in_file = $_SERVER['argv'][1];
$out_file = $_SERVER['argv'][1].'.gz';
$ifh = fopen($in_file,'rb') or die("can't open $in_file: $php_errormsg");
$ofh = fopen($out_file,'wb') or die("can't open $out_file: $php_errormsg");
$encoded = gzencode(fread($ifh,filesize($in_file))) or die("can't encode data: $php_errormsg");
if (-1 == fwrite($ofh,$encoded)) { die("can't write: $php_errormsg"); }
fclose($ofh)or die("can't close $out_file: $php_errormsg");
fclose($ifh)or die("can't close $in_file: $php_errormsg");
The guts of this program are the lines:
$encoded = gzencode(fread($ifh,filesize($in_file)))
or die("can't encode data: $php_errormsg);
if (-1 == fwrite($ofh,$encoded)) { die("can't write: $php_errormsg"); }
The compressed contents of $in_file are stored in $encoded and then written to $out_file with fwrite( ).
You can pass a second argument to gzencode( ) that indicates compression level. Set no compression with 0 and maximum compression with 9. The default level is 1. To adjust the simple gzip program for maximum compression, the encoding line becomes:
$encoded = gzencode(fread($ifh,filesize($in_file)),9) or die("can't encode data: $php_errormsg);
You can also compress and uncompress strings without the gzip-compatibility headers by using gzcompress()and gzuncompress().
Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.
Our monitors are getting better and better and, of course, we want to make them profitable by enjoying images with an increasing resolution when we browse. The problem is that…
MENSAJE
OPERACIÓN EJECUTADA
We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".
These cookies are necessary for the website to function and cannot be disabled on our systems. They are generally only set in response to your actions in requesting services, such as setting your privacy preferences, logging in or completing forms. You can set your browser to block or alert you to these cookies, but some areas of the site will not work. These cookies do not store any personally identifiable information
Performance Cookies
These cookies allow us to count visits and traffic sources so that we can assess the performance of our site and improve it. They help us know which pages are the most or least visited, and how visitors navigate the site. All information collected by these cookies is aggregated and therefore anonymous. If you do not allow these cookies to be used, we will not know when you visited our site and will not be able to assess whether it worked properly
Functional Cookies
These cookies allow the website to provide better functionality and customization. They may be set by our company or by external providers whose services we have added to our pages. If you do not allow these cookies to be used, some of these services may not function properly
Targeted Cookies
These cookies may be set through our site by our advertising partners. They may be used by those companies to profile your interests and display relevant ads on other sites. They do not directly store personal information, but are based on the unique identification of your browser and Internet device. If you do not allow these cookies to be used, you will see less targeted advertising