<?PHP
// loc1 is the path on the computer to the base directory that may be moved
define('loc1', 'T:/home/localhost', true);
// copy a directory and all subdirectories and files (recursive)
// void dircpy( str 'source directory', str 'destination directory' [, bool 'overwrite existing files'] )
function dircpy($source, $dest, $overwrite = false){
if($handle = opendir(loc1 . $source)){ // if the folder exploration is sucsessful, continue
while(false !== ($file = readdir($handle))){ // as long as storing the next file to $file is successful, continue
if($file != '.' && $file != '..'){
$path = $source . '/' . $file;
if(is_file(loc1 . $path)){
if(!is_file(loc1 . $dest . '/' . $file) || $overwrite)
if(!@copy(loc1 . $path, loc1 . $dest . '/' . $file)){
echo '<font color="red">File ('.$path.') could not be copied, likely a permissions problem.</font>';
}
} elseif(is_dir(loc1 . $path)){
if(!is_dir(loc1 . $dest . '/' . $file))
mkdir(loc1 . $dest . '/' . $file); // make subdirectory before subdirectory is copied
dircpy($path, $dest . '/' . $file, $overwrite); //recurse!
}
}
}
closedir($handle);
}
} // end of dircpy()
?>
This new function will be in 0.9.7 (the current release of File Manage) which has just been released 2/2/06.
Hope this helps some people.
makarenkoa at ukrpost dot net
26-Jul-2005 03:58
A function that copies contents of source directory to destination directory and sets up file modes.
It may be handy to install the whole site on hosting.
<?php
// copydirr.inc.php
/*
26.07.2005
Author: Anton Makarenko
makarenkoa at ukrpost dot net
webmaster at eufimb dot edu dot ua
*/
function copydirr($fromDir,$toDir,$chmod=0757,$verbose=false)
/*
copies everything from directory $fromDir to directory $toDir
and sets up files mode $chmod
*/
{
//* Check for some errors
$errors=array();
$messages=array();
if (!is_writable($toDir))
$errors[]='target '.$toDir.' is not writable';
if (!is_dir($toDir))
$errors[]='target '.$toDir.' is not a directory';
if (!is_dir($fromDir))
$errors[]='source '.$fromDir.' is not a directory';
if (!empty($errors))
{
if ($verbose)
foreach($errors as $err)
echo '<strong>Error</strong>: '.$err.'<br />';
return false;
}
//*/
$exceptions=array('.','..');
//* Processing
$handle=opendir($fromDir);
while (false!==($item=readdir($handle)))
if (!in_array($item,$exceptions))
{
//* cleanup for trailing slashes in directories destinations
$from=str_replace('//','/',$fromDir.'/'.$item);
$to=str_replace('//','/',$toDir.'/'.$item);
//*/
if (is_file($from))
{
if (@copy($from,$to))
{
chmod($to,$chmod);
touch($to,filemtime($from)); // to track last modified time
$messages[]='File copied from '.$from.' to '.$to;
}
else
$errors[]='cannot copy file from '.$from.' to '.$to;
}
if (is_dir($from))
{
if (@mkdir($to))
{
chmod($to,$chmod);
$messages[]='Directory created: '.$to;
}
else
$errors[]='cannot create directory '.$to;
copydirr($from,$to,$chmod,$verbose);
}
}
closedir($handle);
//*/
//* Output
if ($verbose)
{
foreach($errors as $err)
echo '<strong>Error</strong>: '.$err.'<br />';
foreach($messages as $msg)
echo $msg.'<br />';
}
//*/
return true;
}
?>