Solved

ZipArchive function problems

Posted on 2010-08-13
6
396 Views
Last Modified: 2012-05-10
I'm having some difficulty with ZipArchive functions in PHP.

In this code, I am getting an error value of 1 which is, for some reason not found in the zipFileErrMsg function.
The file DOES exist in the path passed to the ZipArchive->open() function.

I'm not sure what is going on as this is essentially the same code that I use in another script that works like a charm.

And another thing, I cannot run this script on WAMP because my version of PHP doesn't have php_zip.dll. Any ideas on that?

Can you guys see anything I may be doing wrong?

<?php

function zipFileErrMsg($errno) {
  // using constant name as a string to make this function PHP4 compatible
  $zipFileFunctionsErrors = array(
    'ZIPARCHIVE::ER_MULTIDISK' => 'Multi-disk zip archives not supported.',
    'ZIPARCHIVE::ER_RENAME' => 'Renaming temporary file failed.',
    'ZIPARCHIVE::ER_CLOSE' => 'Closing zip archive failed',
    'ZIPARCHIVE::ER_SEEK' => 'Seek error',
    'ZIPARCHIVE::ER_READ' => 'Read error',
    'ZIPARCHIVE::ER_WRITE' => 'Write error',
    'ZIPARCHIVE::ER_CRC' => 'CRC error',
    'ZIPARCHIVE::ER_ZIPCLOSED' => 'Containing zip archive was closed',
    'ZIPARCHIVE::ER_NOENT' => 'No such file.',
    'ZIPARCHIVE::ER_EXISTS' => 'File already exists',
    'ZIPARCHIVE::ER_OPEN' => 'Can\'t open file',
    'ZIPARCHIVE::ER_TMPOPEN' => 'Failure to create temporary file.',
    'ZIPARCHIVE::ER_ZLIB' => 'Zlib error',
    'ZIPARCHIVE::ER_MEMORY' => 'Memory allocation failure',
    'ZIPARCHIVE::ER_CHANGED' => 'Entry has been changed',
    'ZIPARCHIVE::ER_COMPNOTSUPP' => 'Compression method not supported.',
    'ZIPARCHIVE::ER_EOF' => 'Premature EOF',
    'ZIPARCHIVE::ER_INVAL' => 'Invalid argument',
    'ZIPARCHIVE::ER_NOZIP' => 'Not a zip archive',
    'ZIPARCHIVE::ER_INTERNAL' => 'Internal error',
    'ZIPARCHIVE::ER_INCONS' => 'Zip archive inconsistent',
    'ZIPARCHIVE::ER_REMOVE' => 'Can\'t remove file',
    'ZIPARCHIVE::ER_DELETED' => 'Entry has been deleted',
  );
  $errmsg = 'unknown';
  foreach ($zipFileFunctionsErrors as $constName => $errorMessage) {
    if (defined($constName) and constant($constName) === $errno) {
      return 'Zip File Function error: '.$errorMessage;
    }
  }
  return 'Zip File Function error: unknown';
}
  
require_once "libs/Core_cron.php";
set_time_limit(0);

$user = new User;
$inv = new Inventory;

// This loads a list of records from a table into the $files array
$files = $inv->loadImportFiles();

if(count($files) <= 0)
{
  echo "NO FILES TO IMPORT"."<br>";
  exit;
}
foreach($files as $key=>$file)
{
  $file_temp = $file['file_name'];
  $file['file_name'] = 'file_uploads/'.$file['file_name'];
  $path_parts = pathinfo($file['file_name']);
  $user_id = $file['user_id'];
  if(strtolower($path_parts['extension']) == 'zip') {
    echo "PROCESSING ZIP FILE: ".$file['file_name']."<br>";

    // this shows the correct filename and it does exist in the path.
    echo "FileName: ".$file['file_name']."<br>";

    $zip = new ZipArchive();
    $res = $zip->open($file['file_name']);
    if (is_resource($res)) {
      // Only one file in the zip
      $zippedfile = $zip->statIndex(0);
      $path_parts = pathinfo($zippedfile['name']);

      echo "UNZIPPING FILE: ".$zippedfile['name']."<br>";
      $zip->extractTo('file_uploads/', $zippedfile['name']);
      $zip->close();      
      // delete the zip file
      unlink($file['file_name']);     

      // rename the file to include the user_id and date marker in the filename
      $filename = 'file_uploads/'.$user_id.'_'.$path_parts['filename'].'_'.date('m_d_Y').'.'.$path_parts['extension'];
      rename('file_uploads/'.$zippedfile['name'], $filename);

      // Remove the double line feeds, if any in the file
      $filestr = preg_replace('`[\r\n]+`',"\n", file_get_contents($filename));
      $fp = fopen($filename, "w+");
      fwrite($fp, $filestr);
      fclose($fp);                       
      $file['file_name'] = $filename;
    } else {
      // returning "1:Zip File Function error: unknown"
      die($res.":".zipFileErrMsg($res));
    }
  }
  /* Dont want to do this until I get the file unzipped.
  // import the file
  if($inv->importFile($file['user_id'], $file['file_name'], $file['file_id'], null, true))
  { 
    echo "DONE IMPORTING FILE: ".$file['file_name']."<br>";
  }
  */
}
?>

Open in new window

0
Comment
Question by:EddieShipman
  • 5
6 Comments
 
LVL 6

Expert Comment

by:nitinsawhney
ID: 33432127
Try using absolute path instead of relative for e.g. /var/www/somedir/file_uploads
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 33432275
OK, will use
dir(__FILE__).'file_uploads/'.$file['file_name'];

Open in new window

0
 
LVL 26

Author Comment

by:EddieShipman
ID: 33432342
I meant...
dirname(__FILE__).'file_uploads/'.$file['file_name'];

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Author Comment

by:EddieShipman
ID: 33432369
Well, still didn't work. Output:
PROCESSING ZIP FILE: /home/listinve/public_html/file_uploads/32_InventoryMaster.zip

FileName: /home/listinve/public_html/file_uploads/32_InventoryMaster.zip

1:Zip File Function error: unknown

Open in new window

0
 
LVL 26

Author Comment

by:EddieShipman
ID: 33432406
The file IS a valid zip file as I can open the same file in JZip, 7Zip and WinZip.
The file is also one that was previously processed by my other script that uses
essentially the same code.

That script opens an IMAP mailbox, and takes the zip from the message attachments, copies it
to /file_uploads/temp directory and then opens it and unzips the file to the /file_uploads/ directory.

Been working like a charm for over 2 weeks.

I can't test this locally because my WAMP installation didn't include php_zip.dll and when I try to enable php_zip extension I get Apache errors after the initial error of not being able to find the php_zip.dll file.
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 0 total points
ID: 33445436
Ok, I took some of the code on how to handle the zip file from the PHP docs,
in the comment by saulius at solmetra dot lt here: http://php.net/manual/en/function.zip-open.php.

However, this is where the problem was happening:

if (is_resource($res)) {

It just needed to be like this:
if ($res ===TRUE) {

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
count download link and run update query 9 57
PHP7 and Sql Server Windows 2008 R2 13 86
simplest php form 3 63
Bootstrap collapse causes odd behavior with php loop 7 24
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now