我为我的网站写了一个基本的内容管理系统,包括一个管理面板.我理解基本文件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;
- }