this problem is solved by this script
<?php
/**
* Copies a Directory Contents to new Directory
*/
class copyDir {
var $dir1;
var $dir2;
var $errtxt;
var $overwrite_files;
var $recurse;
var $copy_callback;
var $copy;
function copyDir() {
$this->overwrite_files = false;
$this->recursive = false;
$this->copy_callback = false;
$this->copy = new stdClass();
}
/**
* Set the directory to copy from
*/
function setCopyFromDir( $dir ) {
if (!is_dir($dir)) {
$this->_error('The supplied argument, '.$dir.', is not a valid directory path!');
return false;
}
$this->dir1 = $dir;
return true;
}
/**
* Set the directory to copy to
*/
function setCopyToDir( $dir ) {
if (!is_dir($dir)) {
if (!mkdir($dir)) {
$this->_error('Could not create directory, '.$dir);
return false;
}
}
$this->dir2 = $dir;
return true;
}
/**
* Set if we want to copy sub folders or not
* @param bool TRUE or FALSE
*/
function copySubFolders( $bool = true ) {
$this->recurse = $bool;
}
/**
* Set if we want to overwrite existing files or not
* @param bool TRUE or FALSE
*/
function overWriteFiles( $bool = true ) {
$this->overwrite_files = $bool;
}
/**
* Set a callback function to be executed each time a file or sub folder is copied
* @param string callback function name
*/
function setCopyCallback( $fn ) {
$this->copy_callback = $fn;
}
/**
* Create the directory copy
* @param bool Recurse through sub directories?
*/
function createCopy() {
if ( !is_dir($this->dir1) ) {
$this->_error('Directory to copy from is not a directory. Set the directory using setCopyFromDir()');
return false;
}
if ( !is_dir($this->dir2) ) {
$this->_error('Directory to copy to is not a directory. Set the directory using setCopyToDir()');
return false;
}
// all set, start copy
if ($this->_makeCopy($this->dir1, $this->dir2, $recurse)) {
return true;
}
return false;
}
function _makeCopy($dir1, $dir2) {
if ($dh = opendir($dir1)) {
$i = 0;
$dirArr = array();
while ($el = readdir($dh)) {
$path1 = $dir1.'/'.$el;
$path2 = $dir2.'/'.$el;
$this->_setCopyStatus($path1, $path2);
if (is_dir($path1) && $el != '.' && $el != '..') {
if (is_dir($path2)) {
continue;
}
if (!mkdir($path2)) {
$this->_error('Could not create new directory, '.$path2);
return false;
}
if ($this->recurse) { // copy sub directories recursively
_makeCopy($path1, $path2);
}
} elseif (is_file($path1)) {
if (is_file($path2) && !$this->overwrite_files) {
$this->_error("Duplicate File Exists, when trying to copy file: $path1, to $path2.<br /> Set overWriteFiles() if you need to overwrite existing files.");
return false;
}
if (!copy($path1, $path2)) {
$this->_error('Could not copy file, '.$path1.', to '.$path2);
return false;
}
}
$i++;
}
closedir($dh);
return true;
} else {
$this->_error('Could not open the directory, '.$dir1);
}
return false;
}
function _setCopyStatus($path1, $path2) {
$this->copy->oldfile = $path1;
$this->copy->newfile = $path2;
$this->_callback();
}
function _callback() {
if ($this->copy_callback) {
call_user_func($this->copy_callback, $this->copy);
}
}
function _error($txt) {
$this->errtxt = $txt;
}
/**
* View the last error logged
*/
function viewError() {
return $this->errtxt;
}
}
/**
* Usage of the copy directory class
*/
$dir = dirname(__FILE__); // the directory this script is in
$cp = new copyDir();
// set the directory to copy
if (!$cp->setCopyFromDir( $dir.'/'.$_POST['template'].'/' )) {
echo $cp->viewError();
die;
}
// set the directory to copy to
if (!$cp->setCopyToDir( $dir.'/'.$_POST['webname'].'/' )) {
echo $cp->viewError();
die;
}
if (!$cp->setCopyFromDir( $dir.'/'.$_POST['template'].'/images' )) {
echo $cp->viewError();
die;
}
// set the directory to copy to
if (!$cp->setCopyToDir( $dir.'/'.$_POST['webname'].'/images' )) {
echo $cp->viewError();
die;
}
$cp->copySubFolders(true); // include sub folders when copying
//$cp->overWriteFiles(true); // overwrite existing files
$cp->setCopyCallback( 'updateCopyProgress' );
if (!$cp->createCopy( true )) { // create a copy and recurse through sub folders
echo $cp->viewError();
die;
}
if (!$cp->setCopyFromDir( $dir.'/'.$_POST['template'].'/SpryAssets' )) {
echo $cp->viewError();
die;
}
// set the directory to copy to
if (!$cp->setCopyToDir( $dir.'/'.$_POST['webname'].'/SpryAssets' )) {
echo $cp->viewError();
die;
}
$cp->copySubFolders(true); // include sub folders when copying
//$cp->overWriteFiles(true); // overwrite existing files
$cp->setCopyCallback( 'updateCopyProgress' );
if (!$cp->createCopy( true )) { // create a copy and recurse through sub folders
echo $cp->viewError();
die;
}
if (!$cp->setCopyFromDir( $dir.'/'.$_POST['template'].'/inc' )) {
echo $cp->viewError();
die;
}
// set the directory to copy to
if (!$cp->setCopyToDir( $dir.'/'.$_POST['webname'].'/inc' )) {
echo $cp->viewError();
die;
}
$cp->copySubFolders(true); // include sub folders when copying
//$cp->overWriteFiles(true); // overwrite existing files
$cp->setCopyCallback( 'updateCopyProgress' );
if (!$cp->createCopy( true )) { // create a copy and recurse through sub folders
echo $cp->viewError();
die;
}
// this is our custom function which is called each time we are about to make a new file copy
// or create a new sub directory
function updateCopyProgress(&$copy) {
$file1 = $copy->oldfile;
$file2 = $copy->newfile;
$type = is_dir($file1) ? 'dir' : 'file';
if ($type == 'dir') {
//echo "Creating new Directory, $file2<br />";
} else {
// echo "Copying $file1 to $file2<br />";
}
flush(); // send this to the browser
}
?>
