PHP复制目录功能 [英] PHP Copy Directory Function

查看:192
本文介绍了PHP复制目录功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Minecraft服务器仪表板,其中一个功能是备份和还原您的世界(目录)。我已经有了一个功能(见下文),但是,如你所见,这是一个非常糟糕的代码。任何人都知道更好,更干净的功能?

I'm working on a Minecraft Server Dashboard, and one of it's functions is to backup and restore your world (a directory). I already have a function (see below), but, as you can probably see, it's pretty bad code. Anyone know of a better, cleaner function?

function backupOrRestoreWorld($source,$target){
    foreach(glob($target.'*.*')as$v){
        unlink($v);
    }
    if(is_dir($source)){
        @mkdir($target);
        $d=dir($source);
        while(FALSE!==($entry=$d->read())){
            if($entry=='.'||$entry=='..'){
                continue;
            }
            $Entry=$source.'/'.$entry;
            if(is_dir($Entry)){
                backupOrRestoreWorld($Entry,$target.'/'.$entry);
                continue;
            }
            copy($Entry,$target.'/'.$entry);
        }
        $d->close();
    }
    else{
        copy($source,$target);
    }
    if($source == "server/world"){
        return "World backed up.";
    }
    else {
        return "World restored from backup.";
    }
}


推荐答案

不会在PHP中这样做。只需使用 system(cp -a $ source $ dest)。 (并确保用户不能以任何方式控制 $ source $ dest 的内容,否则您将被黑客攻击。)

I wouldn't do this in PHP. Just use system("cp -a $source $dest"). (And make sure the user can not in any way control the contents of $source and $dest or you will be hacked.)

这篇关于PHP复制目录功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆