Unzipping files automatically using PHP

First off, this tutorial will teach you how to unzip tar.gz file's on your server. Feel free to modify it to suit your needs.

Now Create a php file to check if libraries are ok:

CODE
<?php
foreach ( array('tar','gtar','unzip','gunzip') as $bin) {
exec("whereis $bin"$ret);
print 
join(', '$ret).'<br>';
}
?>




Explain:
foreach( array('tar','gtar','unzip','gunzip') as $bin {
This just checks each file... makes sure it the correct .extention

exec(\"whereis $bin\", $ret);
This executes the program, it finds the file(s) and then gets them ready for extraction

print join(', ', $ret).'< br >';
This tells you its extracting what files.

}
This closes the statement opened with { in foreach()


Okay so now use another one:

CODE
<?php
exec
('tar -xzf NAME_OF_FILE.tar.gz',$ret);
?>



Explain:
exec('tar -xzf NAME_OF_FILE.tar.gz',$ret);
This does all the dirty work for you. It extracts the file.tar.gz onto your sever.


Very, very simple.
Just replace NAME_OF_FILE.tar.gz by your file name, and put this on same directory.
If it timeout due to php maximum execution time, run it again!
It will extract only the missing files.

It's very handy at servage and dreamhost...

Sunday May 27, 2007 | http://www.totaldream.org/article/27-unzipping_files_automatically_using_php.html