2

I am trying to make zip of array of files in the same directory format as it is in my computer the script I've written is working the only issue is that it is overwriting the first element of directory with the second one only the last element in the directory array is going in the zip file I need to add all the files from the directory array provided. Here is my code please tell me what am I doing wrong.

// Get real path for our folder
$rootPaths = array(
               'D:\xampp\htdocs\moko\wp-content\plugins\moko\classes',
               'D:\xampp\htdocs\moko\wp-content\plugins\moko\templates',
             );

$valid_files = array();
if (is_array($rootPaths)) {
     foreach ($rootPaths as $new_files) {
          if (file_exists($new_files)) {
               $valid_files[] = $new_files;
          }
     }
}

// Initialize archive object
if (count($valid_files)) {
    $zip = new ZipArchive();
    $zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
foreach ($valid_files as $rootPath) {
    $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY
    );
}

foreach ($files as $name => $file) {
    // Skip directories (they would be added automatically)
    if (!$file->isDir()) {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}
// Zip archive will be created only after closing object
$zip->close();
}

This script is only creating zip file with the files and directory structure of following directory.

'D:\xampp\htdocs\moko\wp-content\plugins\moko\templates'

Mohsin Abbas
  • 630
  • 8
  • 29

1 Answers1

0

You need not to create a variable $valid_files at all. And the loop you are talking about is to check valid files with in the directories, is only checking that directories in $rootPaths variable exists or not. Also I've changed how you were adding files to zip. Loop was overwriting $files variable every time on execution. Have a look at following code.

// Get real path for our folder
$rootPaths = array(
    'D:\xampp\htdocs\moko\wp-content\plugins\moko\classes',
    'D:\xampp\htdocs\moko\wp-content\plugins\moko\templates',
);

if (is_array($rootPaths)) {
    foreach ($rootPaths as $key => $new_files) {
        if (!file_exists($new_files)) {
            unset($rootPaths[$key]);
        }
    }
}
// Initialize archive object
if (count($rootPaths)) {
    $zip = new ZipArchive();
    $zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
    /** @var SplFileInfo[] $files */
    foreach ($rootPaths as $rootPath) {
        $files = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY
        );
        foreach ($files as $name => $file) {
            // Skip directories (they would be added automatically)
            if (!$file->isDir()) {
                // Get real and relative path for current file
                $filePath = $file->getRealPath();
                $relativePath = substr($filePath, strlen($rootPath) + 1);

                // Add current file to archive
                $zip->addFile($filePath, $relativePath);
            }
        }
    }
// Zip archive will be created only after closing object
    $zip->close();
}
Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
  • I have already tried that solution this makes the zip with all files but do not create any directory I want the directory structure same as it is in my computer – Mohsin Abbas Jul 06 '18 at 09:11
  • I think it should create and this is how I'm using in my code. – Lovepreet Singh Jul 06 '18 at 09:12
  • Yes it is creating the zip as I have tried it earlier before posting here but the thing is it is not creating directories inside the zip let me explain I need zip folder in which there are two folders one is classes and the other one named templates and all the files from classes directory should go inside the classes and same with other. – Mohsin Abbas Jul 06 '18 at 09:15
  • Sorry! What the code is same which code you had changed – Mohsin Abbas Jul 06 '18 at 09:22
  • I had put `foreach ($files as $name => $file)` inside the `foreach ($rootPaths as $rootPath)`. – Lovepreet Singh Jul 06 '18 at 09:23