5

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.

joshkrz
  • 499
  • 3
  • 7
  • 25
  • When you say shell is not an option, do you mean that you cannot call system(), or do you mean that you cannot login to the server with a shell. – DWright Dec 23 '12 at 18:41
  • I cannot shell into the server, I am currently asking my host about system() access. – joshkrz Dec 23 '12 at 18:50
  • Ok, depending on whether your php is allowed to call system(), you could also check whether your host has unzip executable installed. See edit in my answer. – DWright Dec 23 '12 at 18:58
  • Ok, apparently I should be able to call Gzip from PHP and I can actually get SSH access if I formally request it. – joshkrz Dec 23 '12 at 19:02
  • Good, but gzip does not handle .zip format, it handles .gz format. But if you get SSH access, you should be able to install 7zip for yourself. Or `unzip`, if not already installed. – DWright Dec 23 '12 at 19:04
  • OK I will see if that is possible, thanks – joshkrz Dec 23 '12 at 19:06

2 Answers2

6

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.

DWright
  • 9,258
  • 4
  • 36
  • 53
  • Unfortunately 7-zip is not installed on my server. However my host informs me that Gzip is though. – joshkrz Dec 23 '12 at 18:50
  • OK here is the reply I got: "You wouldn't be able to install the software you need via SSH though, no. unzip is a basic tool that is available to Linux systems." – joshkrz Dec 23 '12 at 19:36
  • Great. So if unzip is available, and if you can use system(), you're in business. – DWright Dec 23 '12 at 19:37
  • Ok I have tried this out but I get this in my error logs: [Wed Jan 02 21:57:35 2013] [error] [client 86.0.149.128] caution: filename not matched: -P [Wed Jan 02 21:57:35 2013] [error] [client 86.0.149.128] caution: filename not matched: subw12. Here is my code: $strCommandLine = "unzip test.zip -P subw12"; system($strCommandLine); – joshkrz Jan 02 '13 at 22:04
  • 1
    Looks like I put the password switch in the wrong place. Try putting the password first `$strCommandLine = "unzip -P THEPASSWORD fileToUnzip.zip"`; – DWright Jan 02 '13 at 22:24
  • Ok so I have done: $strCommandLine = "unzip -P subw12 test.zip"; system($strCommandLine); ----- The script returns "Archive: test.zip" but there is nothing in the scripts directory. I have tried adding a destination but the error logs suggest that the script tries opening the directory as an archive. – joshkrz Jan 02 '13 at 22:43
  • 1
    Try adding `-d scripts` at the end, that tells it to extract to the scripts directory. you may need to specify the full path the scripts directory. – DWright Jan 02 '13 at 22:59
6

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

matan
  • 61
  • 1
  • 1