How do you create a .gz file using PHP?

2019-01-10 05:08发布

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

7条回答
We Are One
2楼-- · 2019-01-10 05:56

Also, you could use php's wrappers, the compression ones. With a minimal change in the code you would be able to 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 comment to this answer about zip compression), or to compress.bzip2:// to bzip2 compression.

查看更多
登录 后发表回答