PHP 复制文件夹并重命名 - 完整指南及示例代码
要在 PHP 中复制文件夹并重命名,您可以使用 'recursive' 参数来复制整个文件夹,并使用 'rename' 函数来更改复制后的文件夹名称。下面是一个示例代码:
function copyAndRenameFolder($source, $destination, $newName) {
    if (!is_dir($destination)) {
        mkdir($destination, 0777, true);
    }
    $dir = opendir($source);
    while (false !== ($file = readdir($dir))) {
        if (($file != '.') && ($file != '..')) {
            $srcFilePath = $source . '/' . $file;
            $destFilePath = $destination . '/' . $newName . '/' . $file;
            
            if (is_dir($srcFilePath)) {
                copyAndRenameFolder($srcFilePath, $destFilePath, $newName);
            } else {
                copy($srcFilePath, $destFilePath);
            }
        }
    }
    closedir($dir);
    
    rename($destination, $destination . '/' . $newName);
}
// 使用示例
$sourceFolder = 'path/to/source/folder';
$destinationFolder = 'path/to/destination/folder';
$newFolderName = 'new_folder_name';
copyAndRenameFolder($sourceFolder, $destinationFolder, $newFolderName);
请确保您对目标文件夹具有写入权限。
原文地址: https://www.cveoy.top/t/topic/qlfJ 著作权归作者所有。请勿转载和采集!