Home > php教程 > PHP源码 > 目录复制和删除

目录复制和删除

PHP中文网
Release: 2016-05-25 17:13:22
Original
1238 people have browsed it

目录复制和删除

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);
}
Copy after login

                   

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template