How to Compress a PHP File

How to Compress a PHP File thumbnail
Cut down on load times by compressing the PHP output file.

As a webmaster or website developer, you may notice large files that take many seconds to load for your site visitors. Website developers regularly face this problem. If you are already coding in PHP, then you can decrease the load times by compressing the PHP output file. Though you cannot directly compress the PHP code itself, you can compress the output file that is displayed in the web browser, shaving off many valuable seconds. This is achieved with the Zlib compression package. It is available in PHP 3 and higher versions.

Things You'll Need

  • PHP 3 or higher
Show More

Instructions

    • 1

      Open your terminal on your web server. You will need administrative or root access on your server.

    • 2

      Use one of these commands, depending on which Apache server version you have installed.

      For Apache/1.3.27, type:

      ./configure --without-mysql --with-apxs=/usr/local/apache/bin/apxs --with-zlib

      For Apache/2.0.44, type:

      ./configure --without-mysql --with-apxs2=/usr/local/apache2/bin/apxs --with-zlib

    • 3

      Type "make && make install" on the terminal and hit Enter on your keyboard. This creates and installs the new PHP configuration.

    • 4

      Open the PHP.ini file with your text editor. Normally, this file is located in the PHP installation directory.

    • 5

      Add the following text inside the PHP.ini file:

      output_buffering = On

      output_handler = ob_gzhandler

      zlib.output_compression = Off

      If the settings already exists, but are different, then copy-and-paste the new settings in place of the old.

      Alternatively, you can use this method instead:

      output_buffering = Off

      output_handler =

      zlib.output_compression = On

      Both compression methods are acceptable and the resulting compression is nearly identical.

    • 6

      Save the PHP.ini file. If necessary, upload it back to the same directory you downloaded it.

    • 7

      Open a PHP you want to compress.

    • 8

      Insert this code onto the top line: <?php ob_start("ob_gzhandler"); ?>

    • 9

      Save your PHP file.

    • 10

      View the PHP file in your web browser so you can test the compression and load time.

Tips & Warnings

  • Download and install the latest version of the Zlib compression package so you have the latest technology available on your server.

  • Backup your PHP installation and other important webpage files before attempting this process.

  • The "<?php ob_start("ob_gzhandler"); ?>" code can be automatically inserted into all your PHP files if you make practical use of calling a HEADER.PHP file.

  • Zlib is only compatible with PHP version 3 or higher.

Related Searches:

References

Resources

Comments

You May Also Like

  • How to Compress Multiple Image Files in PHP

    Image manipulation in PHP is best done using the ImageMagick library. This library provides a variety of functions that can be used...

  • How to Compress a File With WinZip

    By compressing, you "zip" the file so that it is smaller and requires less disk space. Zipping a file is especially helpful...

  • How To Attach a PHP File in an Email

    To protect computers from potential viruses, many email providers control the types of attachments that their users are allowed to send and...

  • How to Compress Web Pages

    When optimizing a website to allow for faster load times, one suggested technique is to enable the compression of web pages. Some...

  • How to Compress a PDF File

    While Portable Document Format (PDF) files tend to be smaller than that of the original source document, they can still occupy significant...

  • How to Create a Zip Archive in PHP

    PHP is a versatile computer programming language designed for web functionality. It integrates well with HTML to build powerful software applications that...

  • Quicktime File Types

    Quicktime File Types. Apple's QuickTime digital media platform handles many of today's media file types. While no single media platform manages every...

  • How to Compress IIS Log Files

    Short for Internet Information Services, Microsoft IIS is a collection of software modules and extensions that allow nearly any computer running the...

  • How to Use Bzip2 to Compress Files

    Bzip2 is a Unix command used for file compression. Bzip2 is gaining popularity and is becoming more commonly used than other compression...

  • How to Compress Files Before Sending Them via Email

    If you've been the unwilling recipient of large email attachments, you can understand the value of compressing files before sending them. Both...

  • How to Compress Image Files for a Zip

    Zip files are a special type of file container that can hold other files and folders. Zip files' compression abilities allow you...

  • How to Compress Multiple JPEG Files

    JPEG files, which derive their name from the Joint Photographic Experts group, are compressed photograph and image files. They are the most...

  • How to Compress Files Into a .Zip Archive on a Mac

    Instead of relying on software to help you compress files, Mac OS X has a built-in file compressor that you can access...

  • How to Unzip a File Using PHP

    Compressed--or zipped--files take up much less disk space, but you can't open, read or work with them normally unless you unzip them...

  • How to Batch Compress PDF Files

    Adobe Acrobat software allows you to convert electronic documents of many types to PDF formats, to create new PDFs from scratch and...

Related Ads

Featured