<?
/*
- author: Skakunov Alexander [hide@address.com]
- filename: FileSystemManager.class.php
- date: 28.05.2007
- description: FileSystemManager allows to work with folders at the disk
*/
class FileSystemManager
{
public $folder_src;
public $folder_dst;
public $compare_mtime = true;
public $verbose = false;
public $verbose_separator="<br/>";
public function FileSystemManager($folder_src, $folder_dst="")
{
$this->folder_src = $folder_src;
$this->folder_dst = $folder_dst;
}
public function flush_folder($folder)
{
if(!is_dir($folder))
if(!mkdir($folder))
throw new Exception('Cannot create folder :['.$folder.']');
return true;
}
public function copy_folder($srcdir="", $dstdir="")
{
if(empty($srcdir))
$srcdir = $this->folder_src;
if(empty($dstdir))
$dstdir = $this->folder_dst;
if(empty($dstdir))
return false;
$num = 0;
clearstatcache();
try
{
$this->flush_folder($dstdir);
}
catch(Exception $e)
{
return false;
}
if($curdir = opendir($srcdir))
{
while($file = readdir($curdir))
{
if($file != '.' && $file != '..')
{
$srcfile = $srcdir . DIRECTORY_SEPARATOR . $file;
$dstfile = $dstdir . DIRECTORY_SEPARATOR . $file;
if(is_file($srcfile))
{
if($compare_mtime)
{
if(is_file($dstfile))
{
$ow = filemtime($srcfile) - filemtime($dstfile);
}
else
{
$ow = 1;
}
}
if(!$compare_mtime || $ow > 0) //true if 1) don't compare mtime, 2) compare mtime AND $ow > 0
{
$this->verbose("Copying '".htmlspecialchars($srcfile)."' to '".htmlspecialchars($dstfile)."'...");
if(copy($srcfile, $dstfile))
{
touch($dstfile, filemtime($srcfile));
$num++;
$this->verbose("OK");
}
else
{
$this->verbose("Error: File '".htmlspecialchars($srcfile)."' could not be copied!");
}
}
}
else if(is_dir($srcfile))
{
$num += $this->copy_folder($srcfile, $dstfile);
}
}
}
closedir($curdir);
}
return $num;
}
protected function verbose($message)
{
if($this->verbose)
echo $message . $this->verbose_separator;
}
}
?>