How to compress a file in bzip2 by PHP

0

Hello,

I need to compress files by PHP, and save them to a folder, I searched the web, but I just figured out how to compress a string.

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

    
asked by anonymous 17.07.2016 / 23:37

1 answer

2

In php.net there are examples of how to package with the zip extension and bzip2 . In the previous bzip2 link, the example compresses a string. Then you can read the file as a string. Here's an example of php.net tailored 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 in using the first example, also from php.net.

    
18.07.2016 / 02:24