Gzip files in php

Compress files with Gzip in PHP

If you already want to Gzip compress a file with PHP, the following function will help you. This allows you to store a file in advance gzip-compressed.

How does the Gzip function work?

Um mit dieser Funktion zu arbeiten, benötigen wir den absoluten Server-Pfad zu einer Datei, welchen wir innerhalb der Funktion mit create_gzip( «/../htdocs/ordner/meine-datei.js» ); übergeben.

To work with this function, we need the absolute server path to a file, which we pass to the function with create_gzip( «/../htdocs/folder/my-file.js» ); .

In addition, the compression level can also be customized. The value “w9” is the highest possible compression level. Please note! Not every browser supports the compression type or compression level of Gzip.

Enable automatic gzip compression in .htaccess

Typically, a web host provides a “.htaccess” file for each website as long as the web server is running with Apache. Within this “.htaccess” file, you can also enable automatic Gzip compression for specific file types.

Enable automatic Gzip compression in the php.ini file

Depending on which hosting provider do you have chosen for creating a website, there is also the possibility to enable automatic Gzip compression in the “php.ini” file.

With zlib compression we can compress php scripts or output from php to speed up the loading time of the web page.

Читайте также:  Tensorflow java load model

Disable Gzip compression

The following command allows us to disable automatic gzip compression by the server.

Источник

gzfile

This function is identical to readgzfile() , except that it returns the file in an array.

Parameters

You can set this optional parameter to 1 , if you want to search for the file in the include_path too.

Return Values

An array containing the file, one line per cell, empty lines included, and with newlines still attached, or false on failure.

Examples

Example #1 gzfile() example

See Also

User Contributed Notes 4 notes

In PHP4.4.1 I noticed that gzfile only reads up to 8190 bytes per line. I had a 20K SQL query that was cut into 3 parts — and wondered why the SQL server complained.

Reading an uncompressed file with the file() command works as expected.

A quicker way to load a gziped file in a string :
function gzfile_get_contents ( $filename , $use_include_path = 0 )
//File does not exist
if( !@ file_exists ( $filename ) )

//Read and imploding the array to produce a one line string
$data = gzfile ( $filename , $use_include_path );
$data = implode ( $data );
return $data ;
>
?>

This works similar to gzfile() but it returns the file in a string instead of an array and doesn’t write it to stdout compared to readgzfile.

Note: unlike the usual file-functions filesize won’t work here, since the length-parameter of gzread refers to the uncompressed length, while filesize returns the size of the compressed file.

function gzfile_get_contents ( $filename , $use_include_path = 0 ) <
$file = @ gzopen ( $filename , ‘rb’ , $use_include_path );
if ( $file ) <
$data = » ;
while (! gzeof ( $file )) <
$data .= gzread ( $file , 1024 );
>
gzclose ( $file );
>
return $data ;
>
?>

Источник

gzcompress

This function compresses the given string using the ZLIB data format.

For details on the ZLIB compression algorithm see the document «» ZLIB Compressed Data Format Specification version 3.3» (RFC 1950).

Note:

This is not the same as gzip compression, which includes some header data. See gzencode() for gzip compression.

Parameters

The level of compression. Can be given as 0 for no compression up to 9 for maximum compression.

If -1 is used, the default compression of the zlib library is used which is 6.

One of ZLIB_ENCODING_* constants.

Return Values

The compressed string or false if an error occurred.

Examples

Example #1 gzcompress() example

See Also

  • gzdeflate() — Deflate a string
  • gzinflate() — Inflate a deflated string
  • gzuncompress() — Uncompress a compressed string
  • gzencode() — Create a gzip compressed string

User Contributed Notes 24 notes

Did some simple benchmarking of gzcompress() this morning on a lightly-loaded Fedora 12 server with an AMD Phenom II 940 (quad-core) rocking 8 gigs of ram and executing PHP 5.3.2.2 as an Apache module:

Compression benchmark: (level, time, size (%)):
0: 0.000373 — 82.08 kB (100.02%)
1: 0.000914 — 19.61 kB (23.90%)
2: 0.000951 — 18.88 kB (23.01%)
3: 0.000999 — 18.43 kB (22.46%)
4: 0.001498 — 17.65 kB (21.51%)
5: 0.001744 — 17.09 kB (20.82%)
6: 0.002060 — 16.88 kB (20.57%)
7: 0.002233 — 16.85 kB (20.53%)
8: 0.002808 — 16.71 kB (20.36%)
9: 0.002928 — 16.71 kB (20.36%)

The time code evaluates to:
1. Get start microtime
2. Call gzcompress
3. Get end microtime
4. Report average duration of 100 cycles

The 82.08 kB was a copied and pasted string of two actual, PHP-generated pages we use on our intranet. Running some rough calculations, the time it takes to compress the data at level 9 will never be larger than the time it takes to transmit slightly-less compressed data at levels 6 or lower. In other words, in our application, we have no reason not to fully-compress each PHP script’s output.

GZ-compression works perfectly for html content sent to IE5/IE5.5/Netscape6 browsers. I even compress the

Netscape 4 (on all platforms[?]), IE4/IE5 on the Mac do NOT support gz-compression. Email me if you want to know more.

Ah right, I did not realize it because my browsers (Acorn Browse on RISC OS and Netscape on Windows) don’t make problems at all.

Anyway, you can replace your function «gzip_PrintFourChars»
with «echo pack(«V»,$Crc)».

No, it doesn’t return gzip compressed data — specifically, the CRC is messed up. However, after massaging the output a lot, I have come up with a solution. I also commented it a lot, pointing out odd things.

// Start the output buffer
ob_start ();
ob_implicit_flush ( 0 );

// Get the contents of the output buffer
$contents = ob_get_contents ();
ob_end_clean ();

// Tell the browser that they are going to get gzip data
// Of course, you already checked if they support gzip or x-gzip
// and if they support x-gzip, you’d change the header to say
// x-gzip instead, right?
header ( «Content-Encoding: gzip» );

// Display the header of the gzip file
// Thanks ck@medienkombinat.de!
// Only display this once
echo «\x1f\x8b\x08\x00\x00\x00\x00\x00» ;

// Figure out the size and CRC of the original for later
$Size = strlen ( $contents );
$Crc = crc32 ( $contents );

// Compress the data
$contents = gzcompress ( $contents , 9 );

// We can’t just output it here, since the CRC is messed up.
// If I try to «echo $contents» at this point, the compressed
// data is sent, but not completely. There are four bytes at
// the end that are a CRC. Three are sent. The last one is
// left in limbo. Also, if we «echo $contents», then the next
// byte we echo will not be sent to the client. I am not sure
// if this is a bug in 4.0.2 or not, but the best way to avoid
// this is to put the correct CRC at the end of the compressed
// data. (The one generated by gzcompress looks WAY wrong.)
// This will stop Opera from crashing, gunzip will work, and
// other browsers won’t keep loading indefinately.
//
// Strip off the old CRC (it’s there, but it won’t be displayed
// all the way — very odd)
$contents = substr ( $contents , 0 , strlen ( $contents ) — 4 );

// Show only the compressed data
echo $contents ;

// Output the CRC, then the size of the original
gzip_PrintFourChars ( $Crc );
gzip_PrintFourChars ( $Size );

// Done. You can append further data by gzcompressing
// another string and reworking the CRC and Size stuff for
// it too. Repeat until done.

function gzip_PrintFourChars ( $Val )
<
for ( $i = 0 ; $i < 4 ; $i ++)
<
echo chr ( $Val % 256 );
$Val = floor ( $Val / 256 );
>
>
?>

Источник

Оцените статью