News:

Please note these forums are mostly a testing ground for my SMF work and I don't really use them otherwise.

Main Menu

Paste-1188345926:v:use_geshi-1:v:type-php

Started by SleePy, Aug 29, 2007, 12:05 AM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

SleePy

// Copy a directory to another.
function dircopy($srcdir, $dstdir, $verbose = false)
{
   global $settings;

   $settings['permissions'] = array(
      'chmod' => 0777,
      'do_chown => false,
      'chown => 'www',
      );
/*
   // Permissions
   'permissions' => array(
      'chmod' => 0777,
      'do_chown' => true,
         'chown' => 'www',
   )
*/

   $num = 0;

   // If it doesn't exist. create it.
   if(!is_dir($dstdir))
   {
      mkdir($dstdir, $settings['permissions']['chmod']);

      // Some systems we need to own the files.
      if(!empty($settings['permissions']['do_chown']))
         @chown($dstdir, $settings['permissions']['chown']);

      // Sometimes creating a directory doesn't chmod it.
      @chmod($dstdir, $settings['permissions']['chmod']);
   }

   // Open the directory.
   if($curdir = opendir($srcdir))
   {
      // Loopy loop de-loop.
      while($file = readdir($curdir))
      {
         // No return directories or stupid DS_Store files.
         if($file != '.' && $file != '..' && $file != '.DS_Store')
         {
            // Set the path temporarily.
            $srcfile = $srcdir . '/' . $file;
            $dstfile = $dstdir . '/' . $file;

            // Its a file!
            if(is_file($srcfile))
            {
               // is the destination a file? Check to see if we should update it or not.
               if(is_file($dstfile))
               {
                  $ow = filemtime($srcfile) - filemtime($dstfile);
                  @chmod($dstdir, $settings['permissions']['chmod']);
                  if(!empty($settings['permissions']['do_chown']))
                     @chown($dstdir, $settings['permissions']['chown']);
               }
               else
                  $ow = 1;

               // If we need to update it. Lets do that.
               if($ow > 0)
               {
                  // We speaking?
                  if($verbose)
                     echo "Copying '$srcfile' to '$dstfile'...";

                  // Copy the file hopefully.
                  if(copy($srcfile, $dstfile))
                  {
                     if(!empty($settings['permissions']['do_chown']))
                        @chown($dstdir, $settings['permissions']['chown']);

                     @chmod($dstdir, $settings['permissions']['chmod']);
                     if(!empty($settings['permissions']['do_chown']))
                        @chown($dstdir, $settings['permissions']['chown']);
                     @chmod($srcfile, $settings['permissions']['chmod']);
                     @touch($dstfile, filemtime($srcfile));
                     $num++;
                     if($verbose)
                        echo "OK\n";
                  }
                  else
                     echo "Error: File '$srcfile' could not be copied!\n";
                  }                
               }
            // Hey, we get to loopy - loop all over again.
            else if(is_dir($srcfile))
               $num += dircopy($srcfile, $dstfile, $verbose);
         }
      }
      closedir($curdir);
   }
   return $num;
}
No siggy! :D