PHP compress file

02.Jul.2010

PHP compress is possible since version PHP 3, using zlib. In order to compress file with PHP, just use following function. Function uses two parameters: source of file, and destination of compressed file.

1
2
3
4
5
6
7
8
9
10
function compressFile($sourceFile, $destinationFile)
{
	$file = fopen($sourceFile, 'r');
	$data = fread ($file, filesize($destinationFile));
	fclose($file);
 
	$newFile = gzopen($destinationFile, 'w9');
	gzwrite($newFile, $data);
	gzclose($newFile);
}