Home  >  Article  >  php教程  >  目录复制和删除

目录复制和删除

PHP中文网
PHP中文网Original
2016-05-25 17:13:221205browse

目录复制和删除

function delDir($dirName) {
    if (!file_exists($dirName)) {
        echo 'Folder '. $dirName .' no exist!';
        return false;
    }
    
    $dir = opendir($dirName);
    while ($fileName = readdir($dir)) {
        $file = $dirName . '/' . $fileName;
        if ($fileName != '.' && $fileName != '..') {
            if (is_dir($file)) {
                delDir($file);
            } else {
                unlink($file);
            }            
        }
    }
    closedir($dir);
    rmdir($dirName);    
}

function copyDir($surDir,$toDir) {
    if (!file_exists($surDir)) {
        echo 'Folder ' . $surDir .' no exist!';
        return false;
    }
    
    if (!file_exists($toDir)) {
        mkdir($toDir);
    }
    
    $file = opendir($surDir);
    while ($fileName = readdir($file)) {
        $file1 = $surDir .'/'.$fileName;
        $file2 = $toDir .'/'.$fileName;
        
        if ($fileName != '.' && $fileName != '..') {
            if (is_dir($file1)) {
                copyDir($file1, $file2);        
            } else {
                copy($file1, $file2);
            }
        }
    }
    closedir($file);
}

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn