加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP递归备份脚本

发布时间:2020-12-13 18:23:43 所属栏目:PHP教程 来源:网络整理
导读:我为我的网站写了一个基本的内容管理系统,包括一个管理面板.我理解基本文件IO以及通过 PHP进行复制,但是我对从脚本调用的备份脚本的尝试失败了.我试过这样做: //... authentication,other functionsfor(scandir($homedir) as $buffer){ if(is_dir($buffer))
我为我的网站写了一个基本的内容管理系统,包括一个管理面板.我理解基本文件IO以及通过 PHP进行复制,但是我对从脚本调用的备份脚本的尝试失败了.我试过这样做:
//... authentication,other functions
for(scandir($homedir) as $buffer){
    if(is_dir($buffer)){
        //Add $buffer to an array
    }
    else{
        //Back up the file
    }
}
for($founddirectories as $dir){
    for(scandir($dir) as $b){
        //Backup as above,adding to $founddirectories
    }
}

但它似乎没有用.

我知道我可以使用FTP来做到这一点,但我想要一个完全服务器端的解决方案,只要有足够的授权就可以在任何地方访问.

这是另一种选择:你为什么不 Zip the source directory instead?
function Zip($source,$destination)
{
    if (extension_loaded('zip') === true)
    {
        if (file_exists($source) === true)
        {
            $zip = new ZipArchive();

            if ($zip->open($destination,ZIPARCHIVE::CREATE) === true)
            {
                $source = realpath($source);

                if (is_dir($source) === true)
                {
                    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source),RecursiveIteratorIterator::SELF_FIRST);

                    foreach ($files as $file)
                    {
                        $file = realpath($file);

                        if (is_dir($file) === true)
                        {
                            $zip->addEmptyDir(str_replace($source . '/','',$file . '/'));
                        }

                        else if (is_file($file) === true)
                        {
                            $zip->addFromString(str_replace($source . '/',$file),file_get_contents($file));
                        }
                    }
                }

                else if (is_file($source) === true)
                {
                    $zip->addFromString(basename($source),file_get_contents($source));
                }
            }

            return $zip->close();
        }
    }

    return false;
}

您甚至可以将其解压缩并存档相同的效果,但我必须说我更喜欢以zip文件格式压缩我的备份.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读