Fix for This code will extract files form a zip file uploaded via a specific FileField

  1. <?php
  2. function my_unzip_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  3.   if ($node->type == 'test') {
  4.     dsm($node);
  5.     switch ($op) {
  6.       case 'update':
  7.         if (!is_null($node->field_zipfile[0])) {
  8.           dsm('a zip file is atttached.');
  9.           my_unzip_unzip($node);
  10.         }
  11.         else {
  12.           dsm('No zip file is attached.');
  13.         }
  14.         break;
  15.     } // /switch ($op)
  16.   }
  17. }
  18.  
  19.  
  20. function my_unzip_unzip($node) {
  21.   global $base_path;
  22.  
  23.   $zipfilepath = $node->field_zipfile[0]['filepath'];
  24.   $unzipdir = file_directory_path() . '/nid_' . $node->nid . '/unzipfiles';
  25.   $macdir_in_unzipdir = $unzipdir . '/__MACOSX';
  26.  
  27.   if (!file_exists($unzipdir)){
  28.     mkdir($unzipdir);
  29.   }
  30.  
  31.   $zip = new ZipArchive;
  32.   $res = $zip->open($zipfilepath);
  33.   if ($res === TRUE) {
  34.     $zip->extractTo($unzipdir);
  35.     $zip->close();
  36.     dsm('The attached zip file was decompressed successfully.');
  37.   } else {
  38.     dsm('The decompression was failed.');
  39.   }
  40.  
  41.   if (file_exists($macdir_in_unzipdir)){
  42.     deleteDirectory($macdir_in_unzipdir);
  43.   }
  44. }
  45.  
  46.  
  47. function deleteDirectory($dir) {
  48.   if (!file_exists($dir)) return true;
  49.   if (!is_dir($dir)) return unlink($dir);
  50.   foreach (scandir($dir) as $item) {
  51.     if ($item == '.' || $item == '..') continue;
  52.     if (!deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false;
  53.   }
  54.   return rmdir($dir);
  55. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.