想要备份 PHP 中的代码,其中要作为备份的文件是从另一个指定的(补丁)文件夹中读取的。?

Want to take backup of code in PHP where files which are to be taken as backup is read from another specified (patch)folder.?

我需要为我的项目备份代码,但条件是:-

我通过SVN生成了一个Patch文件夹,里面的文件结构和主工程文件夹中的一样(补丁比主工程文件少)。我想编写一个代码来读取主项目并仅选择补丁中存在的那些文件并将其保存在新的备份文件夹中。

我写的代码是:-

    /*Patch Folder */
     $frmFoldr = 'patch_test/';
     $basepath = '/var/www/html/TestingBackupCron/';


    /*The Folder where files are to be backed up it would use basepath and toFoldr folder name. */
    $toFoldr = 'axisdirectcheck/';

    /* Read/Copy File from this folder. */
    $prodMainFolder = '';
    $prodBasePath = '/var/www/html/TestingBackupCron/sijucheckfiles/';


    $dir_iterator = new RecursiveDirectoryIterator($basepath . $frmFoldr);
    $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $file) {
        $readDir = strstr($file->getPathname(), $frmFoldr);
        $tofileminpath = str_replace($frmFoldr, "", $readDir);

        $fromExPath = $prodBasePath . $tofileminpath;
        $toExPath = $basepath . $toFoldr . $tofileminpath;

        if (strpos($fromExPath, '.php') || strpos($fromExPath, '.js') || strpos($fromExPath, '.css')) {
            if (file_exists($fromExPath)) {
                copy($fromExPath, $toExPath);
                print_r('Files copied to ' . $toExPath . ".\n");
            }else{
                print_r($fromExPath." ::: Read path not found.\n");
            }
        }
    }

上面的代码给出了错误"failed to open stream: No such file or directory"。我认为复制不会创建文件夹。请大家帮忙。

找到解决办法。下面的代码将首先创建文件夹,然后在下一个循环中将复制的文件写入这些文件夹。

public function run($args) {
    /*Patch Folder */
    $frmFoldr = 'patch_test/';
    $basepath = '/var/www/html/TestingBackupCron/';

    /*The Folder where files are to be backed up it would use basepath and toFoldr folder name. */
    $toFoldr = 'axisdirectcheck/';

    /* Read/Copy File from this folder. */
    $prodMainFolder = '';
    $prodBasePath = '/var/www/html/TestingBackupCron/sijucheckfiles/';


    if (file_exists($basepath . $frmFoldr)) {
        $dir_iterator = new RecursiveDirectoryIterator($basepath . $frmFoldr);
        $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
    } else{
        print_r("Read Directory '$basepath$frmFoldr' not Found. Please check the read directory filename.\n");
        exit;
    }
    $fromDir = array();

    $sendToArray = array();

    /* this foreach would create folders inside the path ($toFoldr) specified */
    foreach ($iterator as $file) {
        $readDir = strstr($file->getPathname(), $frmFoldr);
        $tofileminpath = str_replace($frmFoldr, "", $readDir);

        $fromExPath = $basepath . $readDir;
        $toExPath = $basepath . $toFoldr . $tofileminpath;
        $sendToArray[] = $tofileminpath;

        if (strpos($file, '/.')) {
            if (!file_exists($toExPath)) {
                $oldmask = umask(0);
                mkdir("$toExPath", 0777, true);
                umask($oldmask);
            }
        }
    }

    /* this foreach would copy files from PROD/UAT and paste inside the path($toExPath) specified */
    foreach ($sendToArray as $fPath) {
        $fromExPath = $prodBasePath . $fPath;
        $toExPath = $basepath . $toFoldr . $fPath;

        if (strpos($fromExPath, '.php') || strpos($fromExPath, '.js') || strpos($fromExPath, '.css')) {
            if (file_exists($fromExPath)) {
                copy($fromExPath, $toExPath);
                print_r('Files copied to ' . $toExPath . ".\n");
            }else{
                print_r($fromExPath." ::: Read path not found.\n");
            }
        }
    }
}

谢谢..