How to create a .gz file using PHP?

I want gzip to compress a file on my server using PHP. Does anyone have an example that will input a file and output a compressed file?

+50
php gzip compression gz
May 20 '11 at 14:30
source share
8 answers

Other answers here load the entire file into memory during compression, which will lead to out-of-memory errors in large files. The function below should be more reliable in large files when reading and writing files in 512kb blocks.

/** * GZIPs a file on disk (appending .gz to the name) * * From http://stackoverflow.com/questions/6073397/how-do-you-create-a-gz-file-using-php * Based on function by Kioob at: * http://www.php.net/manual/en/function.gzwrite.php#34955 * * @param string $source Path to file that should be compressed * @param integer $level GZIP compression level (default: 9) * @return string New filename (with .gz appended) if success, or false if operation fails */ function gzCompressFile($source, $level = 9){ $dest = $source . '.gz'; $mode = 'wb' . $level; $error = false; if ($fp_out = gzopen($dest, $mode)) { if ($fp_in = fopen($source,'rb')) { while (!feof($fp_in)) gzwrite($fp_out, fread($fp_in, 1024 * 512)); fclose($fp_in); } else { $error = true; } gzclose($fp_out); } else { $error = true; } if ($error) return false; else return $dest; } 
+79
Mar 31 '14 at 5:13
source share

This code does the trick

 // Name of the file we're compressing $file = "test.txt"; // Name of the gz file we're creating $gzfile = "test.gz"; // Open the gz file (w9 is the highest compression) $fp = gzopen ($gzfile, 'w9'); // Compress the file gzwrite ($fp, file_get_contents($file)); // Close the gz file and we're done gzclose($fp); 
+94
May 20 '11 at 14:32
source share

Alternatively, you can use php wrappers compression . With a minimal code change, you can switch between gzip, bzip2 or zip.

 $input = "test.txt"; $output = $input.".gz"; file_put_contents("compress.zlib://$output", file_get_contents($input)); 

change compress.zlib:// to compress.zip:// for zip compression (see the comment on this answer about zip compression) or compress.bzip2:// for bzip2 compression.

+20
May 20 '11 at 14:46
source share

Simple one liner with gzencode () :

 gzencode(file_get_contents($file_name)); 
+5
Dec 04 '14 at 3:49
source share

If you just want to unzip the file, this works and does not cause memory problems:

 $bytes = file_put_contents($destination, gzopen($gzip_path, r)); 
+3
Jun 07 '15 at 16:08
source share

This is probably obvious to many, but if any of the program execution functions is enabled on your system ( exec , system , shell_exec ), you can use them for a simple gzip file.

 exec("gzip ".$filename); 

NB: Be sure to properly clean the $filename variable before using it, especially if it comes from user input (but not only). It can be used to run arbitrary commands, for example, by adding something like my-file.txt && anothercommand (or my-file.txt; anothercommand ).

+1
Feb 09 '17 at 21:10
source share

copy ('file.txt', 'compress.zlib: //'. 'file.txt.gz'); See Documentation

0
Nov 19 '18 at 11:02
source share

Here is an improved version. I got rid of all nested if / else statements, which led to reduced cyclomatic complexity, improved error handling using exceptions instead of tracking the status of a logical error, some types of hints, and I help out if the file has the gz extension already. In terms of lines of code, it has become a little longer, but has become much more readable.

 /** * Compress a file using gzip * * Rewritten from Simon East version here: * https://stackoverflow.com/a/22754032/3499843 * * @param string $inFilename Input filename * @param int $level Compression level (default: 9) * * @throws Exception if the input or output file can not be opened * * @return string Output filename */ function gzcompressfile(string $inFilename, int $level = 9): string { // Is the file gzipped already? $extension = pathinfo($inFilename, PATHINFO_EXTENSION); if ($extension == "gz") { return $inFilename; } // Open input file $inFile = fopen($inFilename, "rb"); if ($inFile === false) { throw new \Exception("Unable to open input file: $inFilename"); } // Open output file $gzFilename = $inFilename.".gz"; $mode = "wb".$level; $gzFile = gzopen($gzFilename, $mode); if ($gzFile === false) { fclose($inFile); throw new \Exception("Unable to open output file: $gzFilename"); } // Stream copy $length = 512 * 1024; // 512 kB while (!feof($inFile)) { gzwrite($gzFile, fread($inFile, $length)); } // Close files fclose($inFile); gzclose($gzFile); // Return the new filename return $gzFilename; } 
0
May 15 '19 at
source share



All Articles