ad box 728 x 90
ad box 468 x 60
Hardware, Software, Code, PHP, ASP, Games, Reviews and Other Funky Stuff
Subscribe

Enter your email address:

Pic of the Month
Popular Posts

Extracting a zip file using PHP and Unzip

Ok here is a problem that has been annoying me for a while. Just in a personal project that im working on, and i finally sat down and figured out how to do it via PHP.

Now this is probably easy and known for most people but if you don’t know how to do it keep reading…

The challenge was, extract multiple files from a passworded zip that’s attached to an email. I know sounds easy, but what if you want the server to do it for you? Now this is running on a hostgator account with default settings and probably execute allowance as well.

After a few trial and errors the following code worked for me, this is the long winded version of it, you can shorten it if you like…:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$zip_name = "file.zip";
$pathtozipfle = "upload-path";
$destinationdir = "upload-path";
$passw = "password";
 
$extract_command = "unzip -P $passw $pathtozipfle/$zip_name -d $pathtozipfle/";
 
echo "Running: $extract_command"; // note probably better and safer to not echo this line as it will reveal the password. But as this is very insecure anyway it probably does not matter too much.
echo "<br><br>";
echo system($extract_command);
echo "<br><br>";
?>

Anyway this extracts file files with no major issues. If you want it to run without returning any data to the screen just run:

1
2
3
<?php
system($extract_command);
?>

If you would like to see what other options are available, and your version of unzip you can run:

?Download unzip.php
1
2
3
<?php
echo system("unzip -Z");
?>

Post Tags

This entry was posted on Monday, August 9th, 2010 at 10:24 am and is filed under HTML, Web. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply

Spam Protection by WP-SpamFree