Unstoppable Domains

Unzipping files with PHP

Spaceship Spaceship
Watch

Keral_Patel

I'll do itRestricted (Chatroom)
Impact
1,449
Well as usual I am again stuck with a single line of code in my development.

I have setup a script which downloads the file automatically every 24hours.

The file is in zip format. So I want to have a peice of code that can unzip that file and extract its content on my webserver.

I tried zip_open(); but it is not working on my server as they don't have that library installed.

Any other ideas would be very helpful.

Thanks.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
Found this on php.net

"If your PHP installation does not have the zip_open function, and you can't install it for whatever reason, you can use these functions instead, if the server has access to the "unzip" utility (most Linux systems do).
So far I have tested these only in Fedora Core 3.
Use at your own risk."

PHP:
<?php

function ShellFix($s)
{
  return "'".str_replace("'", "'\''", $s)."'";
}

function zip_open($s)
{
  $fp = @fopen($s, 'rb');
  if(!$fp) return false;
  
  $lines = Array();
  $cmd = 'unzip -v '.shellfix($s);
  exec($cmd, $lines);
  
  $contents = Array();
  $ok=false;
  foreach($lines as $line)  
  {
   if($line[0]=='-') { $ok=!$ok; continue; }
   if(!$ok) continue;
   
   $length = (int)$line;
   $fn = trim(substr($line,58));
   
   $contents[] = Array('name' => $fn, 'length' => $length);
  }
  
  return
   Array('fp'      => $fp,  
         'name'    => $s,
         'contents' => $contents,
         'pointer'  => -1);
}                          
function zip_read(&$fp)
{
  if(!$fp) return false; 
  
  $next = $fp['pointer'] + 1;
  if($next >= count($fp['contents'])) return false;
 
  $fp['pointer'] = $next;
  return $fp['contents'][$next];
}
function zip_entry_name(&$res)
{
  if(!$res) return false;
  return $res['name'];
}                          
function zip_entry_filesize(&$res)
{
  if(!$res) return false;
  return $res['length'];
}
function zip_entry_open(&$fp, &$res)
{
  if(!$res) return false;

  $cmd = 'unzip -p '.shellfix($fp['name']).' '.shellfix($res['name']);
  
  $res['fp'] = popen($cmd, 'r');
  return !!$res['fp'];  
}
function zip_entry_read(&$res, $nbytes)
{
  return fread($res['fp'], $nbytes);
}
function zip_entry_close(&$res)
{
  fclose($res['fp']);
  unset($res['fp']);
}
function zip_close(&$fp)
{
  fclose($fp['fp']);
}
?>

Hope it helps..
 
0
•••
Yeah i saw this one but it uses zip_open function and that library is not installed on my server.
 
0
•••
The only zip_open is where you are declaring the function.
 
0
•••
that script is not using the zip_open function it is creating it's own so there should be no problem.

Another alternative would be to have a look through http://www.phpclasses.org/
 
0
•••
miseria said:
Found this on php.net

"If your PHP installation does not have the zip_open function, and you can't install it for whatever reason, you can use these functions instead, if the server has access to the "unzip" utility (most Linux systems do).
So far I have tested these only in Fedora Core 3.
Use at your own risk."

PHP:
<?php

function ShellFix($s)
{
  return "'".str_replace("'", "'\''", $s)."'";
}

function zip_open($s)
{
  $fp = @fopen($s, 'rb');
  if(!$fp) return false;
  
  $lines = Array();
  $cmd = 'unzip -v '.shellfix($s);
  exec($cmd, $lines);
  
  $contents = Array();
  $ok=false;
  foreach($lines as $line)  
  {
   if($line[0]=='-') { $ok=!$ok; continue; }
   if(!$ok) continue;
   
   $length = (int)$line;
   $fn = trim(substr($line,58));
   
   $contents[] = Array('name' => $fn, 'length' => $length);
  }
  
  return
   Array('fp'      => $fp,  
         'name'    => $s,
         'contents' => $contents,
         'pointer'  => -1);
}                          
function zip_read(&$fp)
{
  if(!$fp) return false; 
  
  $next = $fp['pointer'] + 1;
  if($next >= count($fp['contents'])) return false;
 
  $fp['pointer'] = $next;
  return $fp['contents'][$next];
}
function zip_entry_name(&$res)
{
  if(!$res) return false;
  return $res['name'];
}                          
function zip_entry_filesize(&$res)
{
  if(!$res) return false;
  return $res['length'];
}
function zip_entry_open(&$fp, &$res)
{
  if(!$res) return false;

  $cmd = 'unzip -p '.shellfix($fp['name']).' '.shellfix($res['name']);
  
  $res['fp'] = popen($cmd, 'r');
  return !!$res['fp'];  
}
function zip_entry_read(&$res, $nbytes)
{
  return fread($res['fp'], $nbytes);
}
function zip_entry_close(&$res)
{
  fclose($res['fp']);
  unset($res['fp']);
}
function zip_close(&$fp)
{
  fclose($fp['fp']);
}
?>

Hope it helps..

I'm going to keep the code. I may use this for my site templating. Thank you miseria!
 
0
•••
Does this code do it automatically?

i need something like this done in will pay
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back