PHP递归备份脚本 [英] PHP Recursive Backup Script
问题描述
我为我的网站编写了一个基本的内容管理系统,包括一个管理面板.我了解基本文件IO以及通过PHP复制的知识,但是我尝试从该脚本调用的备份脚本的尝试失败了.我尝试这样做:
I wrote a basic content-management system for my website, including an administration panel. I understand basic file IO as well as copying via PHP, but my attempts at a backup script callable from the script have failed. I tried doing this:
//... 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来做到这一点,但是我想要一个完全服务器端的解决方案,该解决方案可以在具有足够授权的任何地方进行访问.
I know that I can do this using FTP, but I want a completely server-side solution that can be accessed anywhere with sufficient authorization.
推荐答案
Here is an alternative though: why don't you 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文件格式.
You can even unzip it afterwards and archive the same effect, although I must say I prefer having my backups compressed in zip file format.
这篇关于PHP递归备份脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!