如何在php生成的zip中排除文件
我需要知道如何从php生成的zip中排除文件/文件夹。这是我的密码:如何在php生成的zip中排除文件,php,zip,zlib,Php,Zip,Zlib,我需要知道如何从php生成的zip中排除文件/文件夹。这是我的密码: public function archiveBackup($name = '', $source = '', $destination = '') { if (!extension_loaded('zip') || !file_exists($source)) { return false; } $zip = new ZipArchive(); if (!$zip->op
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”文件,如何做到这一点?如果它工作正常,我可以接受这个答案,但在我的情况下,当文件夹是子文件夹时,它不工作。。。$文件返回的字符串值为:“/home/user\u name/public\u html/folder/translations/bg/fields.php”,如果在数组中键入“bg”,则此操作将不起作用。我用正则表达式完成,然后脚本工作:)我不知道排除的文件夹中是否没有其他文件,这可能会工作:)致意:)
$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));
}
}
}