4

我需要知道如何从 php 生成的 zip 中排除文件/文件夹。这是我的代码:

public function archiveBackup($name = '', $source = '', $destination = '') {
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }
    $zip = new ZipArchive();
    if (!$zip->open($destination.$name, ZIPARCHIVE::CREATE)) {
        return false;
    }
    $source = str_replace('\\', '/', realpath($source));
    if (is_dir($source) === true) {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
        foreach ($files as $file) {
            $file = str_replace('\\', '/', realpath($file));
            if(is_dir($file) === true) {
                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
            }
            else if(is_file($file) === true) {
                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
            }       
        }
    }
    else if (is_file($source) === true) {
        $zip->addFromString(basename($source), file_get_contents($source));
    }
    $zip->close();
    return true;
}

示例:我需要排除文件夹“themes”和“style.css”文件,该怎么做?

4

1 回答 1

4
    $exclude = array('themes', 'style.css');

    foreach ($files as $file) {
      if (!in_array($file, $exclude)) {
        $file = str_replace('\\', '/', realpath($file));
        if(is_dir($file) === true) {
            $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
        }
        else if(is_file($file) === true) {
            $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
        }
      }
    }
于 2012-06-13T23:48:18.950 回答