How to compress a bzip2 file by PHP

Asked

Viewed 143 times

0

Hello,

I need to compress files by PHP, and save them in a folder, searched the web, but only found how to compress a string.

For example: When the user accesses the URL x.php?arquivo=exemplo.jpg PHP will compress the file exemplo.jpg already existing on the server, and will save in the folder compactados.

1 answer

2


In php.net you have examples of how to compress with the extension zip and bzip2. In the previous bzip2 link, the example compresses a string. Then you can read the file as string. Follow example from php.net adapted to your situation:

<?php

$filename = "./testfile.bz2";

// open file for writing
$bz = bzopen($filename, "w");

// write string to file
//Aqui você coloca o caminho para o seu arquivo, para ser lido pelo file_get_contents
bzwrite($bz, file_get_contents('./team-member.jpg'));

// close file
bzclose($bz);

// open file for reading
$bz = bzopen($filename, "r");

// read 10 characters
echo bzread($bz, 10);

// output until end of the file (or the next 1024 char) and close it.  
echo bzread($bz);

bzclose($bz);

?>

You can also zip using the first example, also from php.net.

  • Thanks bro! I searched php.net but couldn’t find :P

Browser other questions tagged

You are not signed in. Login or sign up in order to post.