Recursive Copy of Directory

前端 未结 14 1004
花落未央
花落未央 2020-12-08 15:14

On my old VPS I was using the following code to copy the files and directories within a directory to a new directory that was created after the user submitted their form.

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 15:35

    function recurse_copy($source, $dest)
    {
        // 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);
        }
    
        // Loop through the folder
        $dir = dir($source);
        while (false !== $entry = $dir->read()) {
            // Skip pointers
            if ($entry == '.' || $entry == '..') {
                continue;
            }
    
            // Deep copy directories
            recurse_copy("$source/$entry", "$dest/$entry");
        }
    
        // Clean up
        $dir->close();
        return true;
    }
    

提交回复
热议问题