Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract a ZIP file that has a password using only PHP?

I have seen only one question on here but it does not answer my question. I am running a typical LAMP server that has the most up to date PHP 5 and MYSQL 5 with Redhat Linux.

I need to find a PHP only solution because my host does not allow me to use shell.

Here is my code that extracts ZIPs that are not passworded from vBulletin uploads to another directory:

if ($_GET['add'] == TRUE){
$zip = new ZipArchive;
 $res = $zip->open($SOURCE FOLDER);
 if ($res === TRUE) {
     $zip->extractTo('$DESTINATION FOLDER/');
     $zip->close();
     echo 'File has been added to the library successfuly';
     //Add a flag to that file to indicate it has already been added to the library.
     mysql_query("UPDATE attachment SET library = 1 WHERE filedataid='$fileid'");    
 } else {
     echo 'A uncompression or file error has occured';
 }}

There must be some way to do this using just PHP, surely! Thank you.

UPDATE: My host informs me that gzip is installed on the server but not 7-Zip. I am looking into shell access too.

like image 790
joshkrz Avatar asked Dec 15 '22 16:12

joshkrz


2 Answers

Have 7zip executable available to the script and call it to uncompress the file with the password via system().

$strCommandLine = "7z e fileToUnzip.7z -pTHEPASSWORD";
system($strCommandLine);

You can do something similar with unzip, if your host has that installed. See http://linux.about.com/od/commands/l/blcmdl1_unzip.htm.

It supports -P with a password, so something like this:

$strCommandLine = "unzip fileToUnzip.7z -P THEPASSWORD";
system($strCommandLine);

Caveat: someone could see your password on that command line if they do a ps on the system and see your unzip command running.

like image 163
DWright Avatar answered Jan 12 '23 01:01

DWright


you can use this function setPassword ( string $password ) http://php.net/manual/en/ziparchive.setpassword.php

like image 36
matan Avatar answered Jan 11 '23 23:01

matan