Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用此备份脚本排除文件夹?_Php_Algorithm_File_Zip_Backup - Fatal编程技术网

Php 如何使用此备份脚本排除文件夹?

Php 如何使用此备份脚本排除文件夹?,php,algorithm,file,zip,backup,Php,Algorithm,File,Zip,Backup,我正在使用这个很棒的脚本备份服务器上的文件夹,但是有几个文件夹我想从备份中排除。我怎样才能把他们排除在外呢 谢谢 <?php /* * PHP: Recursively Backup Files & Folders to ZIP-File * (c) 2012-2014: Marvin Menzerath - http://menzerath.eu * contribution: Drew Toddsby */ // Make sure the script can han

我正在使用这个很棒的脚本备份服务器上的文件夹,但是有几个文件夹我想从备份中排除。我怎样才能把他们排除在外呢

谢谢

<?php
/*
 * PHP: Recursively Backup Files & Folders to ZIP-File
 * (c) 2012-2014: Marvin Menzerath - http://menzerath.eu
 * contribution: Drew Toddsby
*/

// Make sure the script can handle large folders/files
ini_set('max_execution_time', 600);
ini_set('memory_limit','1024M');

// Start the backup!
zipData('/var/www/html/uploaded', '/var/www/html/uploaded.zip');
echo 'Finished.';

// Here the magic happens :)
function zipData($source, $destination) {
    if (extension_loaded('zip')) {
        if (file_exists($source)) {
            $zip = new ZipArchive();
            if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
                $source = realpath($source);
                if (is_dir($source)) {
                    $iterator = new RecursiveDirectoryIterator($source);
                    // skip dot files while iterating 
                    $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
                    $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
                    foreach ($files as $file) {
                        $file = realpath($file);
                        if (is_dir($file)) {
                            $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                        } else if (is_file($file)) {
                            $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                        }
                    }
                } else if (is_file($source)) {
                    $zip->addFromString(basename($source), file_get_contents($source));
                }
            }
            return $zip->close();
        }
    }
    return false;
}

输入要排除的路径。如果(!in_array($file,$no_zip){

我试过这种方法

<?php
/*
 * PHP: Recursively Backup Files & Folders to ZIP-File
 * (c) 2012-2014: Marvin Menzerath - http://menzerath.eu
 * contribution: Drew Toddsby
*/

// Make sure the script can handle large folders/files
ini_set('max_execution_time', 600);
ini_set('memory_limit','1024M');

// Start the backup!
zipData('/var/www/html/uploaded', '/var/www/html/uploaded.zip'); //add a third array parameter with names/relative paths of what you want to exclude
echo 'Finished.';

// Here the magic happens :)
function zipData($source, $destination, $nozip = array()) {
    if (is_array($nozip) && extension_loaded('zip')) {
        if (file_exists($source)) {
            $zip = new ZipArchive();
            if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
                $source = realpath($source);
                if (is_dir($source)) {
                    $iterator = new RecursiveDirectoryIterator($source);
                    // skip dot files while iterating 
                    $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
                    $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
                    foreach ($files as $file) {
                        $file = realpath($file);
                        if(!match($file, $nozip)) //Check if it has to zip the file/folder
                        {
                            if (is_dir($file)) {
                                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                            } else if (is_file($file)) {
                                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                            }
                        }
                    }
                } else if (is_file($source)) {
                    $zip->addFromString(basename($source), file_get_contents($source));
                }
            }
            return $zip->close();
        }
    }
    return false;
}


//Returns true if $item is matched once through $array by preg_match()
function match($item, $array)
{
    $matching = array("\\" => "[\/|\\\]", "/" => "[\/|\\\]");
    foreach($array as $i)
    {
        $str = strtr($i, $matching); //creates the regex
        if(preg_match("/".$str."/i", $item))
            return true;
    }

    return false;
}
?>
这里有很好的答案。
这是我的,让你选择最适合你的

我只需添加一个新的var
$exclude
,您就可以设置要排除的文件夹和文件。 例如。 要排除文件夹3、7和文件8/fdsgdfg.js:

<?php
/*
 * PHP: Recursively Backup Files & Folders to ZIP-File
 * (c) 2012-2014: Marvin Menzerath - http://menzerath.eu
 * contribution: Drew Toddsby
*/

// Make sure the script can handle large folders/files
ini_set('max_execution_time', 600);
ini_set('memory_limit','1024M');

$dir = '/var/www/hack/to_bk';

$exclude = array("$dir/3","$dir/7", "$dir/8/fdsgdfg.js");

// Start the backup!
zipData($dir, '/var/www/html/uploaded.zip', $exclude);
echo 'Finished.';
// Here the magic happens :)
function zipData($source, $destination, $exclude) {
    if (extension_loaded('zip')) {
        if (file_exists($source)) {
            $zip = new ZipArchive();
            if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
                $source = realpath($source);
                if (is_dir($source)) {
                    $iterator = new RecursiveDirectoryIterator($source);
                    // skip dot files while iterating 
                    $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
                    $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
                    foreach ($files as $file) {
                        if ( in_array($file, $exclude) ) {
                            continue;
                        }
                        if ( is_file($file) ) {
                            $p = pathinfo($file);
                            if ( in_array($p['dirname'], $exclude) ) {
                                continue;
                            }
                        }
                        $file = realpath($file);
                        if (is_dir($file)) {
                            $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                        } else if (is_file($file)) {
                            $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                        }
                    }
                } else if (is_file($source)) {
                    $zip->addFromString(basename($source), file_get_contents($source));
                }
            }
            return $zip->close();
        }
    }
    return false;
}

尝试在zip函数中添加一组排除的文件夹。当然,排除的文件夹必须是$source的子文件夹

    ini_set('memory_limit','1024M');

    // Start the backup!
    zipData('/var/www/html/uploaded', '/var/www/html/uploaded.zip', array('/var/www/html/uploaded/sb0','/var/www/html/uploaded/sb1','/var/www/html/uploaded/sb2','/var/www/html/uploaded/sb3'));
    echo 'Finished.';

    // Here the magic happens :)
    function zipData($source, $destination, $excluded = array()) {
        if (extension_loaded('zip')) {
            if (file_exists($source)) {
                $zip = new ZipArchive();
                if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
                    $source = realpath($source);
                    if (is_dir($source)) {
                        $iterator = new RecursiveDirectoryIterator($source);
                        // skip dot files while iterating 
                        $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
                        $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
                        foreach ($files as $file) {
                          $file = realpath($file);
                          if (in_array($file,$excluded,true)!=true) { // if (!in_array($file,$excluded,true)) // check if sub-folder is in excluded array.
                            if (is_dir($file)) {
                                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                            } else if (is_file($file)) {
                                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                            }
                          }
                        }
                    } else if (is_file($source)) {
                        $zip->addFromString(basename($source), file_get_contents($source));
                    }
                }
                return $zip->close();
            }
        }
        return false;
    }

你有进展吗?如果我的代码对你有帮助,你可以检查它是否被接受对不起,我没有机会测试它。今天将测试它并让你知道。感谢你花时间帮助。我运行它时只收到一个白色页面,有什么想法吗?修复,你错过了额外的内容)在in_array函数中,我得到了错误,说它需要2个参数。通过在函数中移动$no_zip使其正常工作,现在的问题是它没有排除排除排除文件夹中的所有实际文件。有什么想法吗?@exon用实际状态编辑问题请提前回答这个问题?
    ini_set('memory_limit','1024M');

    // Start the backup!
    zipData('/var/www/html/uploaded', '/var/www/html/uploaded.zip', array('/var/www/html/uploaded/sb0','/var/www/html/uploaded/sb1','/var/www/html/uploaded/sb2','/var/www/html/uploaded/sb3'));
    echo 'Finished.';

    // Here the magic happens :)
    function zipData($source, $destination, $excluded = array()) {
        if (extension_loaded('zip')) {
            if (file_exists($source)) {
                $zip = new ZipArchive();
                if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
                    $source = realpath($source);
                    if (is_dir($source)) {
                        $iterator = new RecursiveDirectoryIterator($source);
                        // skip dot files while iterating 
                        $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
                        $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
                        foreach ($files as $file) {
                          $file = realpath($file);
                          if (in_array($file,$excluded,true)!=true) { // if (!in_array($file,$excluded,true)) // check if sub-folder is in excluded array.
                            if (is_dir($file)) {
                                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                            } else if (is_file($file)) {
                                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                            }
                          }
                        }
                    } else if (is_file($source)) {
                        $zip->addFromString(basename($source), file_get_contents($source));
                    }
                }
                return $zip->close();
            }
        }
        return false;
    }