Hauptmenü

Werkzeuge

Kategorien

Archiv

Verzeichnis rekursiv mit PHP kopieren

Erstellt in PHP am 23. September 2022 vom Daschmi

abstract class DirHelper {

    public static function cpdir(string $source, string $target): void {
        
        $src_dir = opendir($source);
        
        mkdir($target);
        
        while (($file = readdir($src_dir))) {

            if (!in_array($file, ['.', '..'])) {
        
                if (is_dir($source.DIRECTORY_SEPARATOR.$file)) {
            
                    self::cpdir($source.DIRECTORY_SEPARATOR.$file, $target.DIRECTORY_SEPARATOR.$file);
        
                } else {
            
                    copy($source.DIRECTORY_SEPARATOR.$file,$target.DIRECTORY_SEPARATOR.$file);
            
                }

            }

        }
        
        closedir($src_dir);

    }

}