tags:

views:

82

answers:

4

Hello,

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
    }
}

But it did not seem to work.

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.

+2  A: 

You can use recursion.

for(scandir($dir) as $dir_contents){
    if(is_dir($dir_contents)){
        backup($dir_contents);
    }else{
        //back up the file
    }
}
Anon.
+1  A: 

if you have access to execute tar binary file through exec function it would be faster and better i think:

exec('tar -zcvf ' . realpath('some directory') .'/*);

or

chdir('some directory')
exec('tar -zcvf ./*);
+2  A: 

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;
}

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.

Alix Axel
+1  A: 

you got it almost right

$dirs = array($homedir);
$files = array();

while(count($dirs)) {
   $dir = array_shift($dirs);
   foreach(glob("$dir/*") as $e)
      if(is_dir($e)) 
         $dirs[] = $e;
      else
         $files[] = $e;
}
// here $files[] contains all files from $homedir and below

glob() is better than scandir() because of more consistent output

stereofrog