简体   繁体   English

要忽略的递归副本集文件夹

[英]Recursive copy set folder to ignore

I'm using this function to copy parts of WordPress into a backup directory, however when I backup the plugins directory the program enters an infinite loop, is there a way to set a folder to ignore while copying the folders?我正在使用此功能将 WordPress 的部分内容复制到备份目录中,但是当我备份插件目录时,程序进入无限循环,有没有办法在复制文件夹时将文件夹设置为忽略? from echoing out $dir I just get #resource ID x从回显 $dir 我只是得到 #resource ID x

function sisb_content_dir_copy($src, $dst) {
$dir = opendir($src);
// Make the destination directory if not exist
@mkdir($dst);

// Loop through the files in source directory
while( $file = readdir($dir) ) {
  echo $file."</br>";
    if (( $file != '.' ) && ( $file != '..' )){
        if ( is_dir($src . '/' . $file) )
        {

            // Recursively calling custom copy function
            // for sub directory
            sisb_content_dir_copy($src . '/' . $file, $dst . '/' . $file);

        }
        else {
            copy($src . '/' . $file, $dst . '/' . $file);
        }
    }
}

closedir($dir);

} }

As far as you 're running a PHP version greater than 5.4 you can use the PHP built-in DirectoryIterator and FilterIterator classes to achieve your goal.只要您运行的 PHP 版本高于 5.4,您就可以使用 PHP 内置的 DirectoryIterator 和 FilterIterator 类来实现您的目标。 With the Filter class you can declare which files and directories are accepted by the iterator.使用 Filter 类,您可以声明迭代器接受哪些文件和目录。 Beside that the built-in iterator classes are faster and don 't mess around with memory consumption.除此之外,内置的迭代器类更快,并且不会被内存消耗所困扰。 Here 'sa little example.这是一个小例子。

<?php
$directory = new RecursiveDirectoryIterator($path, FilesystemIterator::FOLLOW_SYMLINKS);
$filter = new RecursiveCallbackFilterIterator($directory, function($current, $key, $iterator) {
    // Skip hidden files and directories.
    if ($current->getFilename()[0] === '.') {
        return FALSE;
    }

    if ($current->isDir()) {
        // Only recurse into intended subdirectories
        return $current->getFilename() === 'wanted_dirname';
    } else {
       // Only consume files of interest.
       return strpos($current->getFilename(), 'wanted_filename') === 0;
    }
});

$iterator = new RecursiveIteratorIterator($filter);
$files = [];

foreach ($iterator as $info) {
   $files[] = $info->getPathname();
}

The $files array contains all the files which pass the callback filter. $files数组包含所有通过回调过滤器的文件。 These files you can copy whereever you want.您可以将这些文件复制到任何您想要的位置。 The RecursiveCallbackFilterIterator class is a PHP built-in class, you can use since PHP 5.4 onwards. RecursiveCallbackFilterIterator类是 PHP 内置类,您可以从 PHP 5.4 开始使用。

This example is taken from the PHP documentation for the RecursiveDirectoryIterator class.此示例取自RecursiveDirectoryIterator类的 PHP 文档。 Thanks to sun, who did this six years ago.感谢sun,他在六年前做到了这一点。

Hope that helps.希望有帮助。 ;) ;)

The accepted answer is better as long as you have access to a high enough PHP version, this works for other versions though只要您可以访问足够高的 PHP 版本,接受的答案就会更好,但这适用于其他版本

function sisb_content_dir_copy($src, $dst) {
//echo $src.'</br>';
$dir = opendir($src);
//echo '</br>'.$dst;
// Make the destination directory if not exist
//if (!file_exists('/srv/www/sisb/public_html/wp-content/plugins/WP-SISB/contentbackups')) {
@mkdir($dst);
//}
// Loop through the files in source directory
while( $file = readdir($dir) ) {
  echo $file."</br>";
    if (( $file != '.' ) && ( $file != '..' )){
        if ( is_dir($src . '/' . $file) && ($file !='unwanted folder'))
        {

            // Recursively calling custom copy function
            // for sub directory
            sisb_content_dir_copy($src . '/' . $file, $dst . '/' . $file);

        }
        else {
          if($file !='unwanted folder'){
            copy($src . '/' . $file, $dst . '/' . $file);
          }
        }
    }
}

closedir($dir);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM