title: How to remove __MACOSX while using ZipArchive
body: <p>After hours of trying to figure out why php "ZipArchive" does not work as expected, due to Mac OS adding a "__MACOSX" folder in the zip compression process. </p>
  
  <p>How would I go about deleting "__MACOSX" folder in a zip archive during the upload process in a form?</p>
  
  <p>Below is what I'm working with:</p>
  
  <pre><code>&lt;?php
   public function uploadZip($file)
      {
  
          $advert_index;
  
          $zip = new \ZipArchive;
  
          if (true === $zip-&gt;open($file['tmp_name'])) {
  
              $source = trim($zip-&gt;getNameIndex(0), '/');
  
              for ($i = 0; $i &lt; $zip-&gt;numFiles; $i++) {
                  $name = $zip-&gt;getNameIndex($i);
  
                  // Determine output filename (removing the `$source` prefix).
                  $substring_name = substr($name, strlen($source)+1);
                  $file = $this-&gt;option['uploads_path'] . $this-&gt;option['advert_id'] . '/' . $substring_name;
  
                  // Store the adverts `index.html` file URL to be returned.
                  if ('html' === pathinfo($name, PATHINFO_EXTENSION)) {
                      $advert_index = basename($name);
                  }
  
                  // Create the directories if necessary.
                  $dir = dirname($file);
                  if (! is_dir($dir)) {
                      mkdir($dir, 0777, true);
                  }
  
                  // Read from Zip and write to disk.
                  $fpr = $zip-&gt;getStream($name);
                  $fpw = fopen($file, 'w');
                  while ($data = fread($fpr, 1024)) {
                      fwrite($fpw, $data);
                  }
                  fclose($fpr);
                  fclose($fpw);
              }
              $zip-&gt;close();
  
              return array(
                  'status' =&gt; true,
                  'message' =&gt; array(
                      'index' =&gt; $advert_index,
                      'type' =&gt; 'text/html',  // @HACK: Set MIME type manually. @TODO: Read MIME type from file.
                  ),
              );
          }
  
          return array(
              'status' =&gt; false,
              'message' =&gt; 'Upload failed',
          );
      }
  </code></pre>
  
  <p>Any help would be much appreciated :)</p>
  
  <p><strong>Note: I Managed to find the file just can't get it to be removed.</strong></p>
  
  <pre><code>// Check to see the __MACOSX
  if($zip-&gt;getNameIndex($i) === "__MACOSX/") {
      error_log($zip-&gt;getNameIndex($i) . ' - Error here continue');
      $zip-&gt;deleteName("__MACOSX/");
      continue; // Move on to the next iteration
      // $zip-&gt;deleteIndex($i);
  
  } else {
      $name = $zip-&gt;getNameIndex($i);
  }
  </code></pre>