PHP
- 11/02/2010
Compress files with PHP
Hello folks, today I will show you how to compress file with PHP. The procedure is very simple, if your server is with zlib enabled just run a code like this:
- <?
- / **
- * Inserts a file within a zip.
- *
- * @ Param string $ name name_of_zip ZIP file that will be worked.
- * @ Param string $ path_file Path original file that goes to the ZIP
- * @ Param string $ file_name file name inside the ZIP
- * @ Return Mixed
- * /
- $name_of_zip = null , $path_file = null , $file_name = null ) { fileToZip function ($ name_of_zip = null, $ path_file = null, $ file_name = null) (
- ; return 'All parameters are required';
- )
- / / Create an instance of the ZIP
- ZipArchive; $ Zip = new ZipArchive;
- / / If unable to create the ZIP file
- $zip -> open ( $name_of_zip , ZIPARCHIVE:: CREATE ) === true ) { if ($ zip -> open ($ name_of_zip, ZIPARCHIVE:: CREATE) === true) (
- / / Add the file in the zip
- ( $path_file , $file_name ) ; $ Zip -> AddFile ($ path_file, $ file_name);
- ( ) ; // fecha a conexão com o ZIP $ Zip -> close () / / close the connection to the ZIP
- / / Optionally you can delete the original file, just insert a variable in the parameters
- / / Unlink ("/ path / to / file / file.php ');
- ; return true;
- { Else ()
- ; return false;
- )
- )
- ?>
This post is a complement of How to unzip files with PHP and together we can turn into a useful class.
Let's see if it works? I will play this role and function of the previous post into a file and run to see if the file was created correctly:
- <?
- / **
- * Inserts a file within a zip.
- *
- * @ Param string $ name name_of_zip ZIP file that will be worked.
- * @ Param string $ path_file Path original file that goes to the ZIP
- * @ Param string $ file_name file name inside the ZIP
- * @ Return Mixed
- * /
- $name_of_zip = null , $path_file = null , $file_name = null ) { fileToZip function ($ name_of_zip = null, $ path_file = null, $ file_name = null) (
- ; return 'All parameters are required';
- )
- / / Create an instance of the ZIP
- ZipArchive; $ Zip = new ZipArchive;
- / / If unable to create the ZIP file
- $zip -> open ( $name_of_zip , ZIPARCHIVE:: CREATE ) === true ) { if ($ zip -> open ($ name_of_zip, ZIPARCHIVE:: CREATE) === true) (
- / / Add the file in the zip
- ( $path_file , $file_name ) ; $ Zip -> AddFile ($ path_file, $ file_name);
- ( ) ; // fecha a conexão com o ZIP $ Zip -> close () / / close the connection to the ZIP
- / / Optionally you can delete the original file, just insert a variable in the parameters
- / / Unlink ("/ path / to / file / file.php ');
- ; return true;
- { Else ()
- ; return false;
- )
- )
- / **
- * This function decompresses files
- * A zip
- *
- * @ Param string $ path File Location. Zip
- * @ Param string $ pathunzip folder where the files should be unpacked
- * /
- $path , $pathunzip = '.' ) { function unzip ($ path, $ pathunzip = '.') (
- ===- # Instantiates the class Zip
- ZipArchive; $ Zip = new ZipArchive;
- # Try to open the zip ===-
- $zip -> open ( $path ) ) { if ($ zip -> open ($ path)) (
- -> extractTo ( $pathunzip ) ; // executa o unzip $ Return = $ zip -> extractTo ($ pathunzip) / / run the unzip
- ( ) ; // fecha a coneção com o .zip $ Zip -> close () / / close the conexion with. Zip
- { Else ()
- )
- )
- / **
- * This function returns an array with
- * File names that are
- * In the zip.
- *
- * @ Param string $ path File Location. Zip
- * @ Return array or false on error
- * /
- $path ) { zipToArray function ($ path) (
- # ===- Instantiates class
- ZipArchive; $ Zip = new ZipArchive;
- # Try to open the zip ====-
- $zip -> open ( $path ) ) { if ($ zip -> open ($ path)) (
- # ===- Retrieves the number of zip files
- -> numFiles ; Num_files $ = $ zip -> numFiles;
- # ===- Travels files taking names and putting it in an array
- $i = 0 ; $i <= ( $num_files ) -1 ; $i ++ ) { for ($ i = 0, $ i <= ($ num_files) -1; $ i + +) (
- = $zip -> getNameIndex ( $i ) ; $ Output [] = $ zip -> getNameIndex ($ i);
- )
- # ====- Closes the connection
- ( ) ; $ Zip -> close ();
- # ====- Retore the array to be manipulated
- ; return $ output;
- )
- ; return false;
- )
- / / Add files
- , './texto.txt' , 'texto.txt' ) ; fileToZip ('. / nome_do_zip.zip', '. / Text File.txt', 'Text File.txt');
- , './texto.txt' , 'texto2.txt' ) ; fileToZip ('. / nome_do_zip.zip', '. / Text File.txt', 'texto2.txt');
- / / Read the zip has created to see if any files within
- ?>
See the result:
I'll do a post about creating a class with these features soon.
For today is that ...





1 Comment to "Compress files with PHP"
Marcelão good ... as always rocking in posts!
So he had spoken to had made a generic class, but is not generally not .. lol .. but as it is a little different than yours, follow the code and there suggestions for improvement ... is working well ... but I want to work with the ideology proper OO ... so help me! : P
<? Php
/ **
* Zip Class - For zip files from a direct ¿½ rio
*
* @ Author Rafael Ortega Bueno
* @ Parameter $ path = Folder Path
* @ Parameter name = $ name the new file
** /
(class Zipador
public function zip ($ path, $ folder) (
$ Path = $ path ."/";
$ Size = strlen ("$ folder");
for ($ i = 0, $ i <$ size; $ i + +) (
if (substr ($ folder, $ i, 1) == "/"){
$ FileName = substr ($ folder ($ i +1), $ size);
)
)
/ / - Up Jhonatan
$ Dir = opendir ($ path);
$ X = Array ();
while ($ file = readdir ($ dir)) (
array_push ($ x, $ file);
)
for ($ i = 0; $ iopen ("$ (new_name). zip", ZIPARCHIVE:: CREATE) === true) (
$ Zip-> AddFile ($ path. $ X [$ i], $ x [$ i]);
$ Zip-> close ();
print "File successfully zipped";
)
else (
print "Problems zipping your file";
)
)
)
return " File Download ";
/ / Echo "location.href = '$ new_name.zip'";
)
//------------
public function buscaNomePasta ($ id) (
$ Db = mysql_query ("SELECT file FROM sending
WHERE idenvio = $ id ");
while ($ row = mysql_fetch_array ($ db)) (
$ Folder = $ row ['file'];
)
return $ folder;
)
)
?>
By Rafael Ortega Bueno (6 comments) on 12/02/2010