gpt4 book ai didi

php - 使用php将目录的全部内容复制到另一个目录

转载 作者:IT老高 更新时间:2023-10-28 11:45:09 28 4
gpt4 key购买 nike

我尝试使用

将目录的全部内容复制到另一个位置
copy ("old_location/*.*","new_location/");

但它说它找不到流,真正的 *.* 没有找到。

其他方式

谢谢戴夫

最佳答案

适用于一级目录。对于具有多级目录的文件夹,我使用了这个:

function recurseCopy(
string $sourceDirectory,
string $destinationDirectory,
string $childFolder = ''
): void {
$directory = opendir($sourceDirectory);

if (is_dir($destinationDirectory) === false) {
mkdir($destinationDirectory);
}

if ($childFolder !== '') {
if (is_dir("$destinationDirectory/$childFolder") === false) {
mkdir("$destinationDirectory/$childFolder");
}

while (($file = readdir($directory)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}

if (is_dir("$sourceDirectory/$file") === true) {
recurseCopy("$sourceDirectory/$file", "$destinationDirectory/$childFolder/$file");
} else {
copy("$sourceDirectory/$file", "$destinationDirectory/$childFolder/$file");
}
}

closedir($directory);

return;
}

while (($file = readdir($directory)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}

if (is_dir("$sourceDirectory/$file") === true) {
recurseCopy("$sourceDirectory/$file", "$destinationDirectory/$file");
}
else {
copy("$sourceDirectory/$file", "$destinationDirectory/$file");
}
}

closedir($directory);
}

关于php - 使用php将目录的全部内容复制到另一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2050859/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com