簡體   English   中英

復制整個目錄,但排除一些文件php

[英]copy entire directory but exclude some files php

試圖找到一種復制整個目錄但排除某些文件的方法,在這種情況下,只需要排除一個目錄,該目錄將始終只包含1個文件png ...圖可能會使用類似於此代碼的東西,但絕對不知道如何僅排除文件

function xcopy($source, $dest, $permissions = 0755)
{
    // Check for symlinks
    if (is_link($source)) {
        return symlink(readlink($source), $dest);
    }

    // Simple copy for a file
    if (is_file($source)) {
        return copy($source, $dest);
    }

    // Make destination directory
    if (!is_dir($dest)) {
        mkdir($dest, $permissions);
    }

    // Loop through the folder
    $dir = dir($source);
    while (false !== $entry = $dir->read()) {
        // Skip pointers
        if ($entry == '.' || $entry == '..') {
            continue;
        }

        // Deep copy directories
        xcopy("$source/$entry", "$dest/$entry", $permissions);
    }

    // Clean up
    $dir->close();
    return true;
}

這是一個可能適合您的功能。 我已注明需要澄清:

<?php
    function CopyDirectory($settings = false)
        {
            // The script may take some time if there are lots of files
            ini_set("max_execution_time",3000);
            // Just do some validation and pre-sets
            $directory  =   (isset($settings['dir']) && !empty($settings['dir']))? $settings['dir'] : false;
            $copyto     =   (isset($settings['dest']) && !empty($settings['dest']))? $settings['dest'] : false;
            $filter     =   (isset($settings['filter']) && !empty($settings['filter']))? $settings['filter'] : false;
            // Add the copy to destinations not to copy otherwise
            // you will have an infinite loop of files being copied
            $filter[]   =   $copyto;
            // Stop if the directory is not set
            if(!$directory)
                return;
            // Create a recursive directory iterator
            $dir        =   new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory),RecursiveIteratorIterator::CHILD_FIRST);

            try{ 
                foreach($dir as $file) {
                        $copydest   =   str_replace("//","/",$copyto."/".str_replace($_SERVER['DOCUMENT_ROOT'],"",$file));
                        $compare    =   rtrim($file,".");
                        if(is_dir($file)) {
                                if(!is_dir($copydest)) {
                                        if(!in_array($compare,$filter)) {
                                                if(isset($skip) && !preg_match("!".$skip."!",$file) || !isset($skip))
                                                    @mkdir($copydest,0755,true);
                                                else
                                                    $record[]   =   $copydest;
                                            }
                                        else {
                                                $skip       =   $compare;
                                            }
                                    }
                            }
                        elseif(is_file($file) && !in_array($file,$filter)) {
                                copy($file,$copydest);
                            }
                        else {
                                if($file != '.' && $file != '..')
                                    $record[]   =   $copydest;
                            }
                    }
                }
            // This will catch errors in copying (like permission errors)
            catch (Exception $e)
                {
                    $error[]    =   $e;
                }
        }

    // Copy from
    $settings['dir']        =   $_SERVER['DOCUMENT_ROOT'];
    // Copy to
    $settings['dest']       =   $_SERVER['DOCUMENT_ROOT']."/tester";
    // Files and folders to not include contents
    $settings["filter"][]   =   $_SERVER['DOCUMENT_ROOT'].'/core.processor/';
    $settings["filter"][]   =   $_SERVER['DOCUMENT_ROOT'].'/config.php';
    $settings["filter"][]   =   $_SERVER['DOCUMENT_ROOT'].'/client_assets/images/';

    // Create instance
    CopyDirectory($settings);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM