Copy specific files to a ZIP file in Drupal

Geschreven op 15 mei 2018 door Marcel Kuijk
<?php

// Increase the time limit for closing large zip files.
drupal_set_time_limit(300);

$dir = 'sites/default/files/test';
$target_directory = 'sites/default/files';
$name = 'tmp-archive';

$files = file_scan_directory($dir, '/.*\.html$/');
$archive_uri = $target_directory . '/' . $name . '.zip';

$zip = new ZipArchive();
if ($zip->open($archive_uri, ZipArchive::CREATE) === TRUE) {
  foreach ($files as $file) {
    $file_root = explode('/', $file->uri);
    for ($j = 0; $j < 3; $j++) {
      unset($file_root[$j]);
    }
    $file_dest = implode('/', $file_root);
    $zip->addFile(drupal_realpath($file->uri), $file_dest);
  }
}

$zip->close();

?>

Reactie toevoegen

Plaats hier uw naam.