title: How to compress a folder in yii2 web app into a zip?
body: <p>I am trying to zip a folder.</p>
  <p>This is the function in <code>ExportarController.php</code></p>
  <pre><code>public function zipping(){
          $rootPath = realpath('results/');
  
          // Initialize archive object
          $zip = new \ZipArchive();
          $zip-&gt;open('../web/descargas/Region.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
  
          // Initialize empty &quot;delete list&quot;
          $filesToDelete = array();
  
          // Create recursive directory iterator
          /** @var SplFileInfo[] $files */
          $files = new RecursiveIteratorIterator(
              new RecursiveDirectoryIterator($rootPath),
              RecursiveIteratorIterator::LEAVES_ONLY
          );
  
          foreach ($files as $name =&gt; $file)
          {
              // Skip directories (they would be added automatically)
              if (!$file-&gt;isDir())
              {
                  // Get real and relative path for current file
                  $filePath = $file-&gt;getRealPath();
                  $relativePath = substr($filePath, strlen($rootPath) + 1);
  
                  // Add current file to archive
                  $zip-&gt;addFile($filePath, $relativePath);
  
                  // Add current file to &quot;delete list&quot;
                  // delete it later cause ZipArchive create archive only after calling close function and ZipArchive lock files until archive created)
                  if ($file-&gt;getFilename() != 'important.txt')
                  {
                      $filesToDelete[] = $filePath;
                  }
              }
          }
  
          // Zip archive will be created only after closing object
          $zip-&gt;close();
  
          // Delete all files from &quot;delete list&quot;
          foreach ($filesToDelete as $file)
          {
              unlink($file);
          }
  
      }
  </code></pre>
  <p>It worked pefectly when I tried it outside <code>yii2</code> webapp. But when I added the function to <code>yii2</code> controller I got the error:</p>
  <pre><code>Class 'app\controllers\ZipArchive' not found
  </code></pre>
  <p>Do you know what is going wrong? does it have something to do with the directory I'm trying to write in?</p>
  <p>Please help me.</p>
  <p>Thank you so much.</p>
  <p><strong>SOLUTION</strong></p>
  <p>It was cuz of the namespace, I had to ad \ to every ziparchive function.</p>
  <p>Like \ZipArchive()  and \ZipArchive::CREATE | \ZipArchive::OVERWRITE</p>
  <p>Thanks to all of you.</p>