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.

Disable Gzip compression

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

Compress files with Gzip in PHP Score: 5 / 5 (2votes)